如何检查在iOS中的分辨率,视频的比特率 [英] How to check Resolution, bitrate of video in iOS

查看:5553
本文介绍了如何检查在iOS中的分辨率,视频的比特率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


  1. 获取视频的分辨率和位元率。

  2. 视频。如果它大640x480,我会把这个视频压缩一半,并在1/4调整比特率。例如,如果视频分辨率为1920x1080,则其将被压缩为960x540,并且以4mbps的1920x1080将以1mbps压缩为960x540。

我有几个问题:


  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);

    }];
    }

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

    解决方案

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

      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);


    I'm developing a video compression functionally; my ideas are below:

    1. Getting resolution and bit-rate of video.
    2. Check resolution of video. If it larger 640x480, I will compress this video in half and adjust the bit rate in 1/4 . E.g., if resolution of video is 1920x1080, it will be compressed to 960x540 and 1920x1080 at 4mbps will be compressed to 960x540 at 1mbps.

    I have a few questions:

    1. How can get resolution and bit-rate of video in iOS?
    2. If compress 1920x1080 in half to 960x540, the bit-rate will also adaptively adjust, or do I still need to set the bitrate manually? How can do that?

    I tried below code to compress video but I don't know it compressed to which resolution:

    - (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);
    
         }];
    }
    

    Please give me some advice. Thanks in advance.

    解决方案

    To get the resolution of the video use this :-

    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);
    

    you can get the frameRate and bitrate as follows:-

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

    这篇关于如何检查在iOS中的分辨率,视频的比特率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆