首先,我要祝贺 Brad 在 GPUImage 上的出色工作。
我正在尝试对给定的视频文件应用旋转并获取 mpeg4 (AVFileTypeMPEG4) 文件作为输出。
执行此操作时,我收到以下消息:
* -[AVAssetWriterInput appendSampleBuffer:] 当 outputSettings 不为 nil 时,输入缓冲区必须为未压缩格式
使用以下文件类型设置为 AVFileTypeMPEG4 的 GPUImageMovieWriter 的 init 方法时会出现此问题:
- (id)initWithMovieURL:(NSURL *)newMovieURL size:(CGSize)newSize fileType:(NSString *)newFileType outputSettings:(NSMutableDictionary *)outputSettings;
然而,它适用于另一个 init 方法,它实际上生成了 quicktime 电影(默认设置为 AVFileTypeQuickTimeMovie)。
- (id)initWithMovieURL:(NSURL *)newMovieURL size:(CGSize)newSize;
如果 GPUImageWriter 从现在开始能够输出 mpeg4 电影文件,请有人告诉我。
经过多次调查和尝试,您可以在下面看到我制作的几个代码之一。
movieFile = [[GPUImageMovie alloc] initWithURL:inputUrl];
movieFilter = [[GPUImageBrightnessFilter alloc] init];
[movieFilter setInputRotation:videoRotationMode atIndex:0];
[movieFile addTarget:movieFilter];
unlink([[outputUrl path] UTF8String]); // If a file already exists, AVAssetWriter won't let you record new frames, so delete the old movie
NSMutableDictionary *videoSettings = [[NSMutableDictionary alloc] init];;
[videoSettings setObject:AVVideoCodecH264 forKey:AVVideoCodecKey];
[videoSettings setObject:[NSNumber numberWithInteger:width] forKey:AVVideoWidthKey];
[videoSettings setObject:[NSNumber numberWithInteger:height] forKey:AVVideoHeightKey];
AudioChannelLayout channelLayout;
memset(&channelLayout, 0, sizeof(AudioChannelLayout));
channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
NSDictionary *audioSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[ NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,
[ NSNumber numberWithInt: 2 ], AVNumberOfChannelsKey,
[ NSNumber numberWithFloat: 16000.0 ], AVSampleRateKey,
[ NSData dataWithBytes:&channelLayout length: sizeof( AudioChannelLayout ) ], AVChannelLayoutKey,
[ NSNumber numberWithInt: 32000 ], AVEncoderBitRateKey,
nil];
// Will create quicktime file
movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:outputUrl size:CGSizeMake(640.0, 480.0) fileType:AVFileTypeMPEG4 outputSettings:videoSettings];
[movieFilter addTarget:movieWriter];
// Configure this for video from the movie file, where we want to preserve all video frames and audio samples
movieWriter.shouldPassthroughAudio = NO; // default YES
[movieWriter setHasAudioTrack:TRUE audioSettings:audioSettings];
movieFile.audioEncodingTarget = movieWriter;
[movieFile enableSynchronizedEncodingUsingMovieWriter:movieWriter];
[movieWriter setDelegate:self];
[movieWriter startRecording];
[movieFile startProcessing];
提前致谢
PS:我用的是iOS6