How to check Resolution, bitrate of video in iOS

后端 未结 3 1409
说谎
说谎 2021-02-07 15:24

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 6
相关标签:
3条回答
  • 2021-02-07 15:28

    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))
    }
    
    0 讨论(0)
  • 2021-02-07 15:38

    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!

    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题