2017-12-01 18 views
0

중복되는 질문 일 수 있지만 해결을 위해 많은 시간을 보냈습니다. 다큐멘터리 디렉토리에 mp4 파일을 다운로드합니다. 이 함수를 사용하여 모든 파일 이름을 가져올 수 있습니다.문서 디렉토리에서 비디오 재생 시간을 얻는 방법

func listFilesFromDocumentsFolder() -> [String]? 
    { 
     let fileMngr = FileManager.default; 
     let docs = fileMngr.urls(for: .documentDirectory, in: .userDomainMask)[0].path 
     return try? fileMngr.contentsOfDirectory(atPath:docs) 
    } 

그리고이 파일의 모든 시간 길이를 가져 오려고합니다. 제가하려고 무엇을 보여주지 :

var downs = listFilesFromDocumentsFolder()! 
for i in 0...downs.count - 1{ 
    if(downs[i] == ".DS_Store"){ 
       continue 
    } 
    let fileManager = FileManager.default; 
    let urls = fileManager.urls(for: .documentDirectory, in: .userDomainMask) 
    if let documentDirectory:NSURL = urls.first as! NSURL as! NSURL{ 
     let yourFinalVideoURL = documentDirectory.appendingPathComponent(downs[i]) 
     let asset : AVURLAsset = AVURLAsset(url: yourFinalVideoURL!) as AVURLAsset 
     let totalSeconds = Int(CMTimeGetSeconds(asset.duration)) 
     let minutes = totalSeconds/60 
     let seconds = totalSeconds % 60 
     let mediaTime = String(format:"%02i:%02i",minutes, seconds) 
     print(yourFinalVideoURL) 
     print(mediaTime) 
} 

출력이 내 비디오 파일의 URL을 얻을 때 내가 잘못했던 제 생각에는

Optional(file:///Users/utf8/Library/Developer/CoreSimulator/Devices/D4F341F1-38A2-498B-99F0-076BE9164A5C/data/Containers/Data/Application/718927F7-4E39-43A8-B760-2A468F82A10F/Documents/viki50102klr.mp4) 
00:00 

입니다. 그러나 그것을 고치는 방법. 나는 많은 것을 시도한다. 파일이 있는지 여부도 확인합니다. 그것은 물론 존재합니다.

또한 당신이 NSURL에 URL을 캐스팅 안

+0

당신은 무엇을 어떻게해야합니까? – rmaddy

+0

관련이 없지만 왜 'NSURL'을 사용하여 실제로 모든 나쁜 캐스팅을 했습니까? – rmaddy

+0

안녕 Maddy, 그 0 (영)도 ... – Antiokhos

답변

2

... 어쨌든 이미 솔루션을 주셔서 감사

AVAsset(url: URL.init(url: yourFinalVideoURL)!) 

unfortunetely가 작동하지 않습니다 ..

을 시도합니다. 문서 디렉토리 URL을 가져 와서 파일 이름과 확장명을 추가하십시오. 게다가 당신은 더블 CMTime초 속성을 얻을 제대로 "h m s" 형식으로 시간을 표시 String(format:) 방법을 사용할 수 있습니다 : 당신이`대신 mediaTime``의 totalSeconds`를 인쇄하면

let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! 
let videoURL = documentDirectory.appendingPathComponent(downs[i])  
let duration = AVURLAsset(url: videoURL).duration.seconds 
    print(duration) 
let time: String 
if duration > 3600 { 
    time = String(format:"%dh %dm %ds", 
     Int(duration/3600), 
     Int((duration/60).truncatingRemainder(dividingBy: 60)), 
     Int(duration.truncatingRemainder(dividingBy: 60))) 
} else { 
    time = String(format:"%dm %ds", 
     Int((duration/60).truncatingRemainder(dividingBy: 60)), 
     Int(duration.truncatingRemainder(dividingBy: 60))) 
} 
print(time) 
+1

그래, 그거야. 고맙습니다. 나쁜 nsurl 캐스팅을 수정 주셔서 감사합니다 – Antiokhos

+0

당신은 오신 것을 환영합니다 –