How do make a reduced size video using AVAssetWriter?

后端 未结 2 1442
灰色年华
灰色年华 2021-01-30 15:31

I would make reduced size video, maybe 50 pixel across and 75 pixels for length. Those are the physical dimension.

How do you set that? in the videosettings? I think

2条回答
  •  隐瞒了意图╮
    2021-01-30 16:00

    You need a PhD to work with AVAssetWriter - it's non-trivial: https://developer.apple.com/library/mac/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/05_Export.html#//apple_ref/doc/uid/TP40010188-CH9-SW1

    There's an amazing library for doing exactly what you want which is just an AVAssetExportSession drop-in replacement with more crucial features like changing the bit rate: https://github.com/rs/SDAVAssetExportSession

    Here's how to use it:

    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
    
      SDAVAssetExportSession *encoder = [SDAVAssetExportSession.alloc initWithAsset:[AVAsset assetWithURL:[info objectForKey:UIImagePickerControllerMediaURL]]];
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDirectory = [paths objectAtIndex:0];
      self.myPathDocs =  [documentsDirectory stringByAppendingPathComponent:
                          [NSString stringWithFormat:@"lowerBitRate-%d.mov",arc4random() % 1000]];
      NSURL *url = [NSURL fileURLWithPath:self.myPathDocs];
      encoder.outputURL=url;
      encoder.outputFileType = AVFileTypeMPEG4;
      encoder.shouldOptimizeForNetworkUse = YES;
    
      encoder.videoSettings = @
      {
      AVVideoCodecKey: AVVideoCodecH264,
      AVVideoCompressionPropertiesKey: @
        {
        AVVideoAverageBitRateKey: @2300000,
        AVVideoProfileLevelKey: AVVideoProfileLevelH264High40,
        },
      };
      encoder.audioSettings = @
      {
      AVFormatIDKey: @(kAudioFormatMPEG4AAC),
      AVNumberOfChannelsKey: @2,
      AVSampleRateKey: @44100,
      AVEncoderBitRateKey: @128000,
      };
    
      [encoder exportAsynchronouslyWithCompletionHandler:^
      {
        int status = encoder.status;
    
        if (status == AVAssetExportSessionStatusCompleted)
        {
          AVAssetTrack *videoTrack = nil;
          AVURLAsset *asset = [AVAsset assetWithURL:encoder.outputURL];
          NSArray *videoTracks = [asset tracksWithMediaType:AVMediaTypeVideo];
          videoTrack = [videoTracks objectAtIndex:0];
          float frameRate = [videoTrack nominalFrameRate];
          float bps = [videoTrack estimatedDataRate];
          NSLog(@"Frame rate == %f",frameRate);
          NSLog(@"bps rate == %f",bps/(1024.0 * 1024.0));
          NSLog(@"Video export succeeded");
          // encoder.outputURL <- this is what you want!!
        }
        else if (status == AVAssetExportSessionStatusCancelled)
        {
          NSLog(@"Video export cancelled");
        }
        else
        {
          NSLog(@"Video export failed with error: %@ (%d)", encoder.error.localizedDescription, encoder.error.code);
        }
      }];
    }
    

提交回复
热议问题