Swiftでカメラロールにある動画を取り出した(~.movの)ものをmp4に変換したいと思い調べていると、
下記の記事を見つけて実装してみましたが変換がうまくいかなかったので質問させていただきました。
Swift で録画したビデオ(mov)を MP4 に変換する
コード内の moviePath はString型で既にカメラロールから選択した動画のパスが入っている状態です。
Swift
1// mp4変換 2 let filename = "testVideo.mp4" 3 4 let filePath = NSTemporaryDirectory().stringByAppendingString(filename) 5 let outputURL = NSURL(fileURLWithPath: filePath) 6 7 let myUrl: NSURL = NSURL(string: moviePath)! 8 let avAsset = AVURLAsset(URL: myUrl, options: nil) 9 let exportSession = AVAssetExportSession(asset: avAsset, presetName: AVAssetExportPresetPassthrough) 10 11 exportSession!.outputFileType = AVFileTypeMPEG4 // ファイルタイプの指定 12 exportSession!.outputURL = outputURL // 保管場所を指定 13 14exportSession!.exportAsynchronouslyWithCompletionHandler { () -> Void in 15 do { 16 try NSFileManager.defaultManager().removeItemAtPath(filePath) 17 switch exportSession!.status { 18 case AVAssetExportSessionStatus.Completed: 19 PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(NSURL(fileURLWithPath: self.moviePath)) 20 break 21 case AVAssetExportSessionStatus.Failed: 22 print("failed \(exportSession?.error)") 23 break 24 case AVAssetExportSessionStatus.Cancelled: 25 print("cancelled \(exportSession?.error)") 26 break 27 default: 28 break 29 } 30 } catch { 31 print("Error") 32 } 33 }
実行してもエラーはでないものの、ローカルにも保存されていません。
outputURLで管理場所を指定しているにもかかわらずその場所に~.mp4が入らない原因はなぜでしょうか。
どなたか教えていただけると助かります。
下記のコードがimagePickerController内で上記の内容を行ったものになります。
Swift
1func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 2 let mediaType = info[UIImagePickerControllerMediaType] as! NSString 3 if mediaType == kUTTypeMovie { 4 let urlM: NSURL = info[UIImagePickerControllerMediaURL] as! NSURL 5 } 6 7 let filePath = NSTemporaryDirectory().stringByAppendingString(filename) 8 let filename = "testVideo.mp4" 9 let outputURL = NSURL(fileURLWithPath: filePath) 10 11 let avAsset = AVURLAsset(URL: myUrl, options: nil) 12 let exportSession = AVAssetExportSession(asset: avAsset, presetName: AVAssetExportPresetPassthrough) 13 14 exportSession!.outputFileType = AVFileTypeMPEG4 15 exportSession!.outputURL = outputURL 16 17 exportSession!.exportAsynchronouslyWithCompletionHandler { () -> Void in 18 do { 19 try NSFileManager.defaultManager().removeItemAtPath(filePath) 20 switch exportSession!.status { 21 case AVAssetExportSessionStatus.Completed: 22 PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(NSURL(fileURLWithPath: urlR)) // エラー 23 break 24 case AVAssetExportSessionStatus.Failed: 25 print("failed \(exportSession?.error)") 26 break 27 case AVAssetExportSessionStatus.Cancelled: 28 print("cancelled \(exportSession?.error)") 29 break 30 default: 31 break 32 } 33 } catch { 34 print("Error") 35 } 36 } 37}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/06/29 08:14
2016/06/29 08:19
2016/06/29 08:21 編集
2016/06/29 13:41 編集
2016/06/29 13:53
2016/06/29 14:06 編集
2016/06/30 00:42
2016/06/30 03:01
2016/06/30 03:09
2016/06/30 04:36
2016/06/30 04:57
2016/06/30 05:07
2016/06/30 05:58
2016/06/30 08:19 編集
2016/06/30 08:17
2016/06/30 08:32
2016/06/30 08:54
2016/06/30 09:35 編集
2016/06/30 09:43
2016/06/30 10:27