How to check Resolution, bitrate of video in iOS
13,515
Solution 1
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);
Solution 2
Video resolution in 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))
}
Solutions without preferredTransform
do not return correct values for some videos on the latest devices!
Solution 3
Here is Avt's answer updated and tested for 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))
}
Author by
user3214941
Updated on June 01, 2022Comments
-
user3214941 almost 2 years
I'm developing a video compression functionally; my ideas are below:
- Getting resolution and bit-rate of video.
- 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:
- How can get resolution and bit-rate of video in iOS?
- 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.
-
AnBisw almost 6 yearsThis will almost always return
CGSizeZero
in case of HLS.m3u8
videos. This should be pointed out. -
Code cracker about 5 yearsThis is only working for mp4 videos, is there any way to find bitrate of m3u8 url?