使用 AVFoundation 更改视频帧大小
我正在尝试将视频帧大小更改为方形,即 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] 时,它会抛出异常,提示“AVssetReaderVideoCompositionOutput.videoComposition 需要设置”。
有人请指导我吗?
TIA
I am trying to change the video frame size to square i.e 100 x 100. Here is the code:
- (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];
}
Now the problem is that when it reaches [assetReader startReading], it throws and exception saying "AVAssetReaderVideoCompositionOutput.videoComposition needs to be set".
Anybody please guide me?
TIA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己做的。一探究竟:
I made it myself. Check it out: