Help us understand the problem. What is going on with this article?

Swift で録画したビデオ(mov)を MP4 に変換する

More than 5 years have passed since last update.

iOS におけるビデオ変換

AVAssetExportSession を使用します。

変換時に指定できるフォーマット

たくさんありますがここでは AVFileTypeMPEG4 を使います。設定できるフォーマットはオフィシャルドキュメント参照。(File Format UTIs)

処理の流れ

だいたい以下のようになります。

  1. AVAsset の生成
  2. 出力(変換)フォーマットなど変換の設定
  3. 変換実行(必要であればライブラリに登録)

サンプルコード

下記のコードで iPhone のカメラで録画したビデオを MP4 に変換できました。

環境:iOS 8.1.1, XCode 6.1

let avAsset = AVURLAsset(URL: self.fileMovPathUrl(), options: nil)
let exportSession = AVAssetExportSession(asset: avAsset, presetName: AVAssetExportPresetPassthrough)
exportSession.outputFileType = AVFileTypeMPEG4
exportSession.outputURL = self.fileMp4PathUrl()

exportSession.exportAsynchronouslyWithCompletionHandler { () -> Void in
    NSFileManager.defaultManager().removeItemAtPath(self.filePath(self.mov_extenstion), error: nil)
    switch exportSession.status {
    case AVAssetExportSessionStatus.Completed:
        let assetsLib = ALAssetsLibrary()
        assetsLib.writeVideoAtPathToSavedPhotosAlbum(self.filePathUrl(self.mp4_extenstion), completionBlock: {
             (nsurl, error) -> Void in
        })
        break
    case AVAssetExportSessionStatus.Failed:
        break
    case AVAssetExportSessionStatus.Cancelled:
       break
    default:
        break
    }
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした