将AVI视频转换为MP4视频(以便在iOS设备上播放)

3

我有一个视频文件,是由第三方库生成的,它从远程摄像机录制视频。该API以4fps和352x288px的AVI格式生成视频文件。

我一直在尝试将此视频转换为iOS可播放的格式。我知道iOS只支持有限的视频格式,因此一直在尝试让任何东西都能工作。

我尝试使用以下内容,这是基于iOS Convert AVI to MP4 Format programatically的...

import UIKit
import AVFoundation

class ViewController: UIViewController {

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
  }

  @IBAction func convert(_ sender: Any) {
    makeItSo()
  }

  func makeItSo() {

    guard let inputUrl = Bundle.main.url(forResource: "DVR8-4580V, Channel 7", withExtension: "avi") else {
      print("Could not find video source")
      return
    }

    let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
    let documentsDirectory = paths[0]
    let filePath = URL(fileURLWithPath: documentsDirectory).appendingPathComponent("MyVideo.mp4").absoluteString
    let outputURL = URL(fileURLWithPath: filePath)
    print("OutputURL = \(outputURL)")
    convertVideoToLowQuailty(withInputURL: inputUrl, outputURL: outputURL, handler: { exportSession in
      guard let exportSession = exportSession else {
        print("No export session")
        return
      }
      print("exportSession.status = \(exportSession.status)")
      if exportSession.status == .completed {
        // Video conversation completed
        print("Video converstion completed")
      } else {
        print("Error = \(exportSession.error)")
      }
    })
  }

  func convertVideoToLowQuailty(withInputURL inputURL: URL?, outputURL: URL?, handler: @escaping (AVAssetExportSession?) -> Void) {
    if let anURL = outputURL {
      try? FileManager.default.removeItem(at: anURL)
    }
    var asset: AVURLAsset? = nil
    if let anURL = inputURL {
      asset = AVURLAsset(url: anURL, options: nil)
    }
    var exportSession: AVAssetExportSession? = nil
    if let anAsset = asset {
      exportSession = AVAssetExportSession(asset: anAsset, presetName: AVAssetExportPresetPassthrough)
    }
    exportSession?.outputURL = outputURL
    exportSession?.outputFileType = .mp4
    exportSession?.exportAsynchronously(completionHandler: {
      handler(exportSession)
    })
  }

}

而其他几个变体都失败了。上述代码会出现以下错误:
Error = Optional(Error Domain=AVFoundationErrorDomain Code=-11822 "Cannot Open" UserInfo={NSLocalizedFailureReason=This media format is not supported., NSLocalizedDescription=Cannot Open, NSUnderlyingError=0x283244bd0 {Error Domain=NSOSStatusErrorDomain Code=-16976 "(null)"}})

我认为要么是我没有为转换器提供正确的选项/建议,要么就是AVFoundation不支持这种类型的操作。

以下是由MediaInfo生成的信息。

General
Complete name                            : /Users/swhitehead/Downloads/DVR8-4580V, Channel 8.avi
Format                                   : AVI
Format/Info                              : Audio Video Interleave
File size                                : 361 KiB
Duration                                 : 28s 500ms
Overall bit rate                         : 104 Kbps

Video
ID                                       : 0
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : Baseline@L2
Format settings                          : 1 Ref Frames
Format settings, CABAC                   : No
Format settings, ReFrames                : 1 frame
Format settings, GOP                     : M=1, N=4
Codec ID                                 : H264
Duration                                 : 28s 500ms
Bit rate                                 : 101 Kbps
Width                                    : 352 pixels
Height                                   : 288 pixels
Display aspect ratio                     : 1.222
Frame rate                               : 4.000 fps
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.249
Stream size                              : 351 KiB (97%)

问题

有没有办法将这种类型的视频文件转换为支持iOS播放的mp4格式?

注意:我知道这可能是一个广泛的问题,因为可能不存在简单的解决方案,但即使有一些建议或提示也会让我感激不尽,因为它让我感到很疯狂。


@rmaddy,我很感激这不是一个技术上基于Objective-C的问题,但如果它能帮助我找到解决问题的方法,我会接受基于Objective-C的答案。 - MadProgrammer
1个回答

0

使用没有“格式设置,GOP:M=1,N=4”的视频。

当我使用没有GOP格式设置的视频时,可以成功导出。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接