如何检查iOS中的分辨率,视频比特率

时间:2014-03-04 02:13:38

标签: ios objective-c cocoa-touch avfoundation avassetwriter

我正在开发视频压缩功能;我的想法如下:

  1. 获取视频的分辨率和比特率。
  2. 检查视频的分辨率。如果它更大640x480,我会将此视频压缩一半,并将比特率调整为1/4。例如,如果视频分辨率是1920x1080,它将被压缩到960x540,19xps的1920x1080将被压缩到960x540,速度为1mbps。
  3. 我有几个问题:

    1. 如何在iOS中获得视频的分辨率和比特率?
    2. 如果将1920x1080压缩到一半到960x540,比特率也会自适应调整,还是需要手动设置比特率?怎么办?
    3. 我尝试使用下面的代码来压缩视频,但我不知道它压缩到了哪个分辨率:

      - (void)convertVideoToLowQuailtyWithInputURL:(NSURL*)inputURL
                                         outputURL:(NSURL*)outputURL
                                           handler:(void (^)(AVAssetExportSession*))handler
      {
          [[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil];
          AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
      
          AVAssetExportSession *session = [[AVAssetExportSession alloc] initWithAsset: urlAsset presetName:AVAssetExportPresetLowQuality];
          session.outputURL = outputURL;
          session.outputFileType = AVFileTypeQuickTimeMovie;
          [session exportAsynchronouslyWithCompletionHandler:^(void)
           {
               handler(session);
      
           }];
      }
      

      请给我一些建议。提前谢谢。

3 个答案:

答案 0 :(得分:23)

要获得视频的分辨率,请使用: -

AVAssetTrack *videoTrack = nil;
AVURLAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:originalVideo]];
NSArray *videoTracks = [asset tracksWithMediaType:AVMediaTypeVideo];

CMFormatDescriptionRef formatDescription = NULL;
NSArray *formatDescriptions = [videoTrack formatDescriptions];
if ([formatDescriptions count] > 0)
    formatDescription = (CMFormatDescriptionRef)[formatDescriptions objectAtIndex:0];

if ([videoTracks count] > 0)
    videoTrack = [videoTracks objectAtIndex:0];

CGSize trackDimensions = {
    .width = 0.0,
    .height = 0.0,
};
trackDimensions = [videoTrack naturalSize];

int width = trackDimensions.width;
int height = trackDimensions.height;
NSLog(@"Resolution = %d X %d",width ,height);

您可以按如下方式获取frameRate和bitrate: -

float frameRate = [videoTrack nominalFrameRate];
float bps = [videoTrack estimatedDataRate];
NSLog(@"Frame rate == %f",frameRate);
NSLog(@"bps rate == %f",bps);

答案 1 :(得分:6)

Swift中的视频分辨率:

func resolutionForLocalVideo(url:NSURL) -> CGSize?
{
    guard let track = AVAsset(URL: url).tracksWithMediaType(AVMediaTypeVideo).first else { return nil }
    let size = CGSizeApplyAffineTransform(track.naturalSize, track.preferredTransform)
    return CGSize(width: fabs(size.width), height: fabs(size.height))
}

没有preferredTransform的解决方案不会为最新设备上的某些视频返回正确的值!

答案 2 :(得分:3)

以下是Avt的答案更新并测试了Swift 3:

func resolutionForLocalVideo(url:URL) -> CGSize?
{
  guard let track = AVURLAsset(url: url).tracks(withMediaType: AVMediaTypeVideo).first else { return nil }
  let size = track.naturalSize.applying(track.preferredTransform)
  return CGSize(width: fabs(size.width), height: fabs(size.height))
}