gpt4 book ai didi

ios5 - 如何合并视频和音频文件?

转载 作者:行者123 更新时间:2023-12-02 05:07:40 25 4
gpt4 key购买 nike

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

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

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

-(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 通过问题中的编辑回答。转换为社区维基答案。参见 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.Delete the following code:

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

b.(optional)Rewrite completion handler:

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

c.Then wait long enough for the program to complete writing.

关于ios5 - 如何合并视频和音频文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9329316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com