使用AVAsset制作带有图片数组和歌曲文件的电影文件

我正在尝试使用图片数组和音频文件制作电影文件。为了制作带有图片阵列的电影,我在这里用了祖尔的大贴子。一切都太完美了,我的电影里有我的照片。但是,当我尝试添加一些音轨时,会有很多问题。为了理解我把我的代码:


当我调用此方法时,图片数组和歌曲文件已准备就绪:


-(void) writeImagesToMovieAtPath:(NSString *) path withSize:(CGSize) size

{

    NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSArray *dirContents = [[NSFileManager defaultManager] directoryContentsAtPath:documentsDirectoryPath];

    for (NSString *tString in dirContents) {

        if ([tString isEqualToString:@"essai.mp4"]) 

        {

            [[NSFileManager defaultManager]removeItemAtPath:[NSString stringWithFormat:@"%@/%@",documentsDirectoryPath,tString] error:nil];


        }

    }


    NSLog(@"Write Started");


    NSError *error = nil;


    AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:

                                  [NSURL fileURLWithPath:path] fileType:AVFileTypeMPEG4

                                                              error:&error];    

    NSParameterAssert(videoWriter);


    NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:

                                   AVVideoCodecH264, AVVideoCodecKey,

                                   [NSNumber numberWithInt:size.width], AVVideoWidthKey,

                                   [NSNumber numberWithInt:size.height], AVVideoHeightKey,

                                   nil];


    AudioChannelLayout channelLayout;

    memset(&channelLayout, 0, sizeof(AudioChannelLayout));

    channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;

音频文件的AssetWriterInput的状态始终为“ NO”。

我的问题:如何使用AVFoundation将音频添加到视频文件?

因此,请有人告诉我是否忘记某事或某事有误,可以帮助我。

非常感谢你


一只斗牛犬
浏览 506回答 3
3回答

陪伴而非守候

AVChannelLayoutKey应该指向一个包含AudioChannelLayout的NSData实例。您指向一个NSNumber。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

iOS