【问题标题】:How to merge video and audio files?如何合并视频和音频文件?
【发布时间】:2012-03-08 21:58:39
【问题描述】:

我的问题是:如何合并持续时间几乎相同的视频和音频文件?

我搜索并得到了这个问题的一些答案。但是,当我尝试他们提供的代码时,它不会产生“非零字节”电影。

你能看一下它,看看哪里出错了吗?

-(void)putTogether
{
NSLog(@"Starting to put together all the files!");

AVMutableComposition *mixComposition = [AVMutableComposition composition];

NSString *audioPath = @"/Users/admin/Documents/Sound.caf";
NSURL *audioUrl = [NSURL fileURLWithPath:audioPath];
//AVURLAsset *audioasset = [AVURLAsset URLAssetWithURL:audioUrl options:nil];
AVURLAsset *audioasset = [[AVURLAsset alloc]initWithURL:audioUrl options:nil];

NSString *videoPath = @"/Users/admin/Documents/video.mp4";
NSURL *videoUrl = [NSURL fileURLWithPath:videoPath];
//AVURLAsset *videoasset = [AVURLAsset URLAssetWithURL:videoUrl options:nil];
AVURLAsset *videoasset = [[AVURLAsset alloc]initWithURL:videoUrl options:nil];

NSString *moviepath = @"/Users/admin/Documents/fmovie.mov";
NSURL *movieUrl = [NSURL fileURLWithPath:moviepath];

if([[NSFileManager defaultManager] fileExistsAtPath:moviepath])
{
    [[NSFileManager defaultManager] removeItemAtPath:moviepath error:nil];
}

AVMutableCompositionTrack *compositionTrackB = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack *clipVideoTrackB = [[videoasset tracksWithMediaType:AVMediaTypeVideo] lastObject];
[compositionTrackB insertTimeRange:CMTimeRangeMake(      kCMTimeZero, videoasset.duration)  ofTrack:clipVideoTrackB atTime:kCMTimeZero error:nil];

AVMutableCompositionTrack *compositionTrackA = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack *clipAudioTrackA = [[audioasset tracksWithMediaType:AVMediaTypeAudio] lastObject];
[compositionTrackA insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioasset.duration)  ofTrack:clipAudioTrackA atTime:kCMTimeZero error:nil];

AVAssetExportSession *exporter =[[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
//AVAssetExportSession *exporter =[AVAssetExportSession exportSessionWithAsset:mixComposition presetName:AVAssetExportPresetLowQuality];
NSParameterAssert(exporter!=nil);
exporter.outputFileType=AVFileTypeQuickTimeMovie;
exporter.outputURL=movieUrl;
CMTime start=CMTimeMake(0, 600);
CMTime duration=CMTimeMake(600, 600);
CMTimeRange range=CMTimeRangeMake(start, duration);
exporter.timeRange=range;
[exporter exportAsynchronouslyWithCompletionHandler:nil];
}

【问题讨论】:

  • 感谢@user1192409。这篇文章对我很有帮助。反正只有一个问题可以知道什么时候写完。?
  • 嗨,你能帮帮我吗?我已使用此代码,但无法在合并文件中获取视频。我能够获得assetSession 的 AVAssetExportSessionStatusCompleted 状态,但看不到视频。我检查了我的视频文件,它是正确的..我的音频文件也是正确的......但是合并两个视频时没有显示......你能告诉我可能是什么问题吗??

标签: ios5 avassetwriter avassetexportsession avcomposition


【解决方案1】:

(由 OP 通过问题中的编辑回答。转换为社区 wiki 答案。请参阅Question with no answers, but issue solved in the comments (or extended in chat)

OP 写道:

我解决了我的问题。

对于和我有同样问题的人,这里是简单的解决方案:

a.删除以下代码:

CMTime start=CMTimeMake(0, 600);
CMTime duration=CMTimeMake(600, 600);
CMTimeRange range=CMTimeRangeMake(start, duration);
exporter.timeRange=range;

b.(可选)重写完成处理程序:

[exporter exportAsynchronouslyWithCompletionHandler:^{ 
    switch ([exporter status]) {
        case AVAssetExportSessionStatusFailed:
            NSLog(@"Export failed: %@", [[exporter error] localizedDescription]);
            break;
        case AVAssetExportSessionStatusCancelled:
            NSLog(@"Export canceled");
            break;
        default:
            break;
    } 
}];

c.然后等待足够长的时间让程序完成写入。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-22
    • 2019-11-20
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    • 2016-05-30
    相关资源
    最近更新 更多