【问题标题】:Changing video Frame size using AVFoundation使用 AVFoundation 更改视频帧大小
【发布时间】:2011-10-29 05:05:28
【问题描述】:

我正在尝试将视频帧大小更改为正方形,即 100 x 100。这是代码:

- (void) changeSize :(NSURL *) url
{

//Create audio/video Settings
NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                               AVVideoCodecH264, AVVideoCodecKey,
                               [NSNumber numberWithInt: 100], AVVideoWidthKey, 
                               [NSNumber numberWithInt:100], AVVideoHeightKey, 
                               nil];

NSDictionary * audioSettings = [NSDictionary dictionaryWithObjectsAndKeys: 
                               [NSNumber numberWithFloat: 44100.0], AVSampleRateKey, 
                               [NSNumber numberWithInt: 2], AVNumberOfChannelsKey, 
                               [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey, 
                               [NSData data], AVChannelLayoutKey, nil];



//Read Asset 
AVURLAsset *myAsset = [AVURLAsset URLAssetWithURL:url options:nil];

NSArray *videoTracks = [myAsset tracksWithMediaType:AVMediaTypeVideo];
AVAssetTrack *audioTrack = [[myAsset tracksWithMediaType:AVMediaTypeAudio]objectAtIndex:0];


AVAssetReader *assetReader = [[AVAssetReader alloc]initWithAsset:myAsset error:nil];

AVAssetReaderOutput *videoOutput = [AVAssetReaderVideoCompositionOutput assetReaderVideoCompositionOutputWithVideoTracks:videoTracks videoSettings:nil];

AVAssetReaderOutput *audioOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:audioTrack outputSettings:nil];

[assetReader addOutput:videoOutput];
[assetReader addOutput:audioOutput];


//Create writer and set its properties
AVAssetWriterInput* writerInputVideo = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo
                                                                           outputSettings:videoSettings] retain];

AVAssetWriterInput* writerInputAudio = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio
                                                                           outputSettings:nil] retain];


NSError *error = nil;
AVAssetWriter *writer = [[AVAssetWriter alloc]initWithURL:[NSURL fileURLWithPath:fullPath] fileType:AVFileTypeQuickTimeMovie error:&error];

[writer addInput:writerInputVideo];
[writer addInput:writerInputAudio];


//start reading/writting
[writer startWriting];
[assetReader startReading];

}

现在的问题是,当它到达 [assetReader startReading] 时,它会抛出异常“需要设置 AVAssetReaderVideoCompositionOutput.videoComposition”。

有人请指导我吗?

TIA

【问题讨论】:

    标签: iphone avfoundation video-recording video-editing core-video


    【解决方案1】:

    我自己做的。看看吧:

    - (void) resizeVideo:(NSURL *)videoPath outputPath:(NSURL *)outputPath
    {
    
    NSURL *fullPath = outputPath;
    
    NSURL *path = videoPath;
    
    
    NSLog(@"Write Started");
    
    NSError *error = nil;
    
    AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:fullPath fileType:AVFileTypeQuickTimeMovie error:&error];    
    NSParameterAssert(videoWriter);
    
    NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                   AVVideoCodecH264, AVVideoCodecKey,
                                   [NSNumber numberWithInt:100], AVVideoWidthKey,
                                   [NSNumber numberWithInt:100], AVVideoHeightKey,
                                   nil];
    
    AVAssetWriterInput* videoWriterInput = [[AVAssetWriterInput
                                             assetWriterInputWithMediaType:AVMediaTypeVideo
                                             outputSettings:videoSettings] retain];
    
    NSParameterAssert(videoWriterInput);
    NSParameterAssert([videoWriter canAddInput:videoWriterInput]);
    
    videoWriterInput.expectsMediaDataInRealTime = NO;
    
    [videoWriter addInput:videoWriterInput];
    
    
    
    
    AVAsset *avAsset = [[AVURLAsset alloc] initWithURL:path options:nil];
    NSError *aerror = nil;
    AVAssetReader *reader = [[AVAssetReader alloc] initWithAsset:avAsset error:&aerror];
    
    
    AVAssetTrack *videoTrack = [[avAsset tracksWithMediaType:AVMediaTypeVideo]objectAtIndex:0];
    
    NSDictionary *videoOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey];
    
    AVAssetReaderTrackOutput *asset_reader_output = [[AVAssetReaderTrackOutput alloc] initWithTrack:videoTrack outputSettings:videoOptions];    
    
    [reader addOutput:asset_reader_output];
    
    
    [videoWriter startWriting];
    [videoWriter startSessionAtSourceTime:kCMTimeZero];
    [reader startReading];
    
    CMSampleBufferRef buffer;
    
    
    while ( [reader status]==AVAssetReaderStatusReading )
    {
        if(![videoWriterInput isReadyForMoreMediaData])
            continue;
    
        buffer = [asset_reader_output copyNextSampleBuffer];
    
    
        NSLog(@"READING");
    
        if(buffer)
            [videoWriterInput appendSampleBuffer:buffer];
    
        NSLog(@"WRITTING...");
    
    
    }
    
    
    //Finish the session:
    [videoWriterInput markAsFinished];  
    [videoWriter finishWriting];
    NSLog(@"Write Ended");
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-24
      • 2020-11-06
      • 1970-01-01
      • 1970-01-01
      • 2015-07-03
      • 1970-01-01
      • 2017-02-25
      • 1970-01-01
      相关资源
      最近更新 更多