2014-03-04 127 views
13

我正在開發視頻壓縮功能;我的想法如下:如何檢查iOS中視頻的分辨率,比特率

  1. 獲取視頻的分辨率和比特率。
  2. 檢查視頻的分辨率。如果它大於640x480,我會將此視頻壓縮一半,並將比特率調整爲1/4。例如,如果視頻分辨率是1920×1080,則它將被壓縮到960×540,並且在4mbps下的1920×1080將以1mbps被壓縮到960×540。

我有幾個問題:

  1. 如何能在iOS視頻的分辨率和比特率?
  2. 如果將1920x1080的一半壓縮到960x540,比特率也會自適應調整,還是需要手動設置比特率?怎麼可以做到這一點?

我想下面的代碼來壓縮視頻,但我不知道它壓縮至分辨率:

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

    }]; 
} 

請給我一些建議。提前致謝。

回答

21

爲了獲得視頻使用的分辨率是: -

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

,你可以得到的幀率和比特率如下: -

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

感謝這麼多。這非常有幫助 – user3214941

6

視頻分辨率斯威夫特:

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的解決方案不會爲最新設備上的某些視頻返回正確的值!

3

這裏是AVT的回答更新和測試雨燕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)) 
}