如何合并视频和音频文件? [英] How to merge video and audio files?
问题描述
我的问题是:如何合并持续时间几乎相同的视频和音频文件?
My question is: How to merge video and audio files that has almost the same duration?
我搜索并得到了一些问题的答案。然而,当我尝试他们提供的代码时,它只是不会产生非零字节电影。
I searched and got some answers to this question. However when I try the code they gave, it just does not produce a "non-zero byte" movie.
你能不能看看它,看看它去了哪里错误?
Could you take a look at it and see where it went wrong?
-(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];
}
推荐答案
(由OP回答问题中的编辑。转换为社区维基答案。请参阅没有答案的问题,但问题在评论中得到解决(或在聊天中扩展))
(Answered by the OP by an edit in the question. Converted to a community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) )
OP写道:
我解决了我的问题。
I solved my problem.
对于那些和我一样有问题的人,这里是简单的解决方案:
For the ones who have same problem as I did, here is the simple solution:
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。等待足够长的时间让程序完成写作。
这篇关于如何合并视频和音频文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!