简体   繁体   English

在objective-c(iOS)中转码视频的最佳方法是什么?

[英]What is the best way to transcode a video in objective-c (iOS)?

I would like to, depending on the device and the settings in my application, transcode a video to a specific video format. 我想,根据我的应用程序中的设备和设置,将视频转码为特定的视频格式。 For an example, if the user has an iPhone 4S and chooses medium settings in my application I would like to convert the video to 540p before I start processing. 例如,如果用户有iPhone 4S并在我的应用程序中选择中等设置,我想在开始处理之前将视频转换为540p。 If he chooses high then I would like to transcode to 720p. 如果他选择高,那么我想转码为720p。

I could read the video frame by frame, resize and save to disc but this does not seem very effective. 我可以逐帧读取视频,调整大小并保存到光盘但这似乎不是很有效。 What would be the easiest and fastest way to transcode a video that I can feed to my video processing libraries? 什么是最简单,最快速的方式来转换我可以提供给我的视频处理库的视频?

I have tried using the videoQuality settings on my UIImagePickerController but seems like it is not working as even when I set it to UIImagePickerControllerQualityTypeIFrame960x540 my video comes out as 720p (640x480 is working but I need to be more granular). 我已尝试在我的UIImagePickerController上使用videoQuality设置,但似乎它不起作用,即使我将其设置为UIImagePickerControllerQualityTypeIFrame960x540我的视频出现为720p(640x480正在工作,但我需要更细粒度)。

You might want to look at AVAssetExportSession, which makes it reasonably simple to re-encode videos. 您可能希望查看AVAssetExportSession,这使得重新编码视频变得相当简单。 I think it's also hardware-supported when possible like the rest of AVFoundation: 我认为它也可以像AVFoundation的其余部分一样支持硬件:

https://developer.apple.com/library/ios/#DOCUMENTATION/AudioVideo/Conceptual/AVFoundationPG/Articles/01_UsingAssets.html https://developer.apple.com/library/ios/#DOCUMENTATION/AudioVideo/Conceptual/AVFoundationPG/Articles/01_UsingAssets.html

Note that it will never make the video larger than it already is, so you aren't guaranteed to get the output size you request. 请注意,它永远不会使视频大于现有视频,因此无法保证获得您请求的输出大小。 The following code might be a start for what you want, assuming you have an instance of ALAsset: 假设您有一个ALAsset实例,以下代码可能是您想要的开头:

- (void)transcodeLibraryVideo:(ALAsset *)libraryAsset 
        toURL:(NSURL *)fileURL 
        withQuality:(NSString *quality) {
  // get a video asset for the original video file
  AVAsset *asset = [AVAsset assetWithURL:
    [NSURL URLWithString:
      [NSString stringWithFormat:@"%@", 
        [[libraryAsset defaultRepresentation] url]]]];
  // see if it's possible to export at the requested quality
  NSArray *compatiblePresets = [AVAssetExportSession 
    exportPresetsCompatibleWithAsset:asset];
  if ([compatiblePresets containsObject:quality]) {
    // set up the export
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]
      initWithAsset:asset presetName:quality];
    exportSession.outputURL = fileURL;
    exportSession.outputFileType = AVFileTypeQuickTimeMovie;
    // run the export
    [exportSession exportAsynchronouslyWithCompletionHandler:^{
      switch ([exportSession status]) {
        case AVAssetExportSessionStatusFailed:
            //TODO: warn of failure
            break;
        case AVAssetExportSessionStatusCancelled:
            //TODO: warn of cancellation
            break;
        default:
            //TODO: do whatever is next
            break;
      }
      [exportSession release];
    }];
  }
  else {
    //TODO: warn that the requested quality is not available
  }
}

You would want to pass a quality of AVAssetExportPreset960x540 for 540p and AVAssetExportPreset1280x720 for 720p, for example. 例如,您可能希望为540p传递AVAssetExportPreset960x540的质量,为720p传递AVAssetExportPreset1280x720的质量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Objective-C 移植到 C++ 的最佳方法是什么? - What is the best way to port from Objective-C to C++? 在Objective-C中实现此滚动UI的最佳方法是什么? - What is the best way to implement this scrolling UI in Objective-C? 在Objective-C中创建常量的最佳方法是什么 - What is the best way to create constants in Objective-C 在Objective-C中以特定间隔增加NSDateComponents的最佳方法是什么? - What is the best way to increment NSDateComponents in specific intervals in Objective-C? 在objective-c协议中定义字符串常量的最佳方法是什么? - What is the best way to define string constants in an objective-c protocol? 解决Objective-C名称空间冲突的最佳方法是什么? - What is the best way to solve an Objective-C namespace collision? 在Objective-C中处理相关的分层数据的最佳方法是什么? - What is the best way work with related, hierarchical data in Objective-C? 将对象传递给Objective-C中另一个类的最佳方法是什么? - What is the best way to pass an object to another class in Objective-C? 硬编码这些数据的最佳方法是什么? Objective-C的 - What is the best way of hardcoding this data. Objective-C 退出和处理 Objective-C 中的致命错误的最佳方法是什么? - What is the best way to exit and handle a fatal error in Objective-C?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM