在iOS中将视频文件与其原始音频合并 [英] Merge video files with their original audio in iOS

查看:392
本文介绍了在iOS中将视频文件与其原始音频合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要合并两个视频文件及其相应的音频。
我尝试使用:
http://www.raywenderlich.com/13418/how-to-play-record-edit-videos-in-ios

I need to merge two video files along with their corresponding audio. I tried using : http://www.raywenderlich.com/13418/how-to-play-record-edit-videos-in-ios

视频获取合并,但他们的音频丢失了。这就像一个静音的视频。我还需要合并音频。我也用Google搜索,找不到任何有用的东西。有人可以帮我这个吗...

The videos gets merged, but their audio is missing. It's like a muted video. I need the audio also to be merged. I also googled on this, couldn't find anything useful. Can someone help me on this please...

推荐答案

实时编辑音频就像现场编辑视频一样。回到每部电影并获取音轨并将其粘贴到你的可变组合中。

Live-editing audio is exactly like live-editing video. Go back to each movie and fetch the audio track and stick it into your mutable composition.

在这个例子中,我抓住前5秒的视频和最后5秒来自电影的视频并将它们一个接一个地放在一个新视频中:

In this example, I grab the first five seconds of video and the last five seconds of video from a movie and put them one after the other in a new video:

NSString* type = AVMediaTypeVideo;
NSArray* arr = [oldAsset tracksWithMediaType:type];
AVAssetTrack* track = [arr lastObject];
CMTime duration = track.timeRange.duration;
AVMutableComposition* comp = [AVMutableComposition composition];
AVMutableCompositionTrack* comptrack = [comp addMutableTrackWithMediaType:type preferredTrackID:kCMPersistentTrackID_Invalid];
[comptrack insertTimeRange:CMTimeRangeMake(CMTimeMakeWithSeconds(0,600), CMTimeMakeWithSeconds(5,600)) ofTrack:track atTime:CMTimeMakeWithSeconds(0,600) error:nil];
[comptrack insertTimeRange:CMTimeRangeMake(CMTimeSubtract(duration, CMTimeMakeWithSeconds(5,600)), CMTimeMakeWithSeconds(5,600)) ofTrack:track atTime:CMTimeMakeWithSeconds(5,600) error:nil];

但结果视频将保持沉默。所以我也回去取相应的音频:

But the resulting video would be silent. So I also go back and fetch the corresponding audio:

type = AVMediaTypeAudio;
arr = [oldAsset tracksWithMediaType:type];
track = [arr lastObject];
comptrack = [comp addMutableTrackWithMediaType:type preferredTrackID:kCMPersistentTrackID_Invalid];
[comptrack insertTimeRange:CMTimeRangeMake(CMTimeMakeWithSeconds(0,600), CMTimeMakeWithSeconds(5,600)) ofTrack:track atTime:CMTimeMakeWithSeconds(0,600) error:nil];
[comptrack insertTimeRange:CMTimeRangeMake(CMTimeSubtract(duration, CMTimeMakeWithSeconds(5,600)), CMTimeMakeWithSeconds(5,600)) ofTrack:track atTime:CMTimeMakeWithSeconds(5,600) error:nil];

这篇关于在iOS中将视频文件与其原始音频合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆