Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios AVMutableComposition减少最终视频的大小和分辨率_Ios_Avmutablecomposition - Fatal编程技术网

Ios AVMutableComposition减少最终视频的大小和分辨率

Ios AVMutableComposition减少最终视频的大小和分辨率,ios,avmutablecomposition,Ios,Avmutablecomposition,我用一组图像创建了一个视频 它成功创建了视频,然后我将音频添加到创建的视频文件中 我创建AVMuatableComposition对象,通过创建AVAssetTracks添加视频和音频,最后在AvassetExportSession的帮助下导出为单个视频文件 假设第一个没有音频的视频是vdo.mp4,final(添加音频后)是final.mp4,那么my final.mp4的大小和分辨率都低于vdo.mp4 这是我的代码,它结合了这两个文件 NSAutoreleasePool *pool = [

我用一组图像创建了一个视频

它成功创建了视频,然后我将音频添加到创建的视频文件中

我创建AVMuatableComposition对象,通过创建AVAssetTracks添加视频和音频,最后在AvassetExportSession的帮助下导出为单个视频文件

假设第一个没有音频的视频是vdo.mp4,final(添加音频后)是final.mp4,那么my final.mp4的大小和分辨率都低于vdo.mp4

这是我的代码,它结合了这两个文件

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSError * error = nil;

AVMutableComposition * composition = [AVMutableComposition composition];

NSURL *url = [NSURL fileURLWithPath:filePath];

AVURLAsset * videoAsset = [AVURLAsset URLAssetWithURL:url options:nil];

AVAssetTrack * videoAssetTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo
                                                                            preferredTrackID: kCMPersistentTrackID_Invalid];

[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,videoAsset.duration) ofTrack:videoAssetTrack atTime:kCMTimeZero error:&error];

CMTime audioStartTime = kCMTimeZero;
for (   NSInteger i = 0;i< [mArrAudioFileNames count];i++ )
{
    NSString *audioFileName = nil;
    NSString *docsDir = nil;
    if ( [mArrAudioFileNames objectAtIndex:i] != [NSNull null]) {
        audioFileName = [mArrAudioFileNames objectAtIndex:i];
        docsDir = [[self dataFolderPathForAudio] stringByAppendingPathComponent:audioFileName];

    }else{
        //audioFileName = @" ";
        docsDir = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"mp3"];
    }

    // NSString *docsDir = [[self dataFolderPathForAudio] stringByAppendingPathComponent:audioFileName];
    AVURLAsset * urlAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:docsDir] options:nil];

    AVAssetTrack * audioAssetTrack = [[urlAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
    AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio
                                                                                preferredTrackID: kCMPersistentTrackID_Invalid];

    [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,urlAsset.duration) ofTrack:audioAssetTrack atTime:audioStartTime error:&error];

    Float64 duration = CMTimeGetSeconds(urlAsset.duration);

    audioStartTime = CMTimeAdd(audioStartTime, CMTimeMake((int) ((duration * kRecordingFPS) + 0.5), kRecordingFPS));
}

AVAssetExportSession* assetExport = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetMediumQuality];
//assetExport.videoComposition = compositionVideoTrack;

 assetExport.outputFileType = AVFileTypeQuickTimeMovie;// @"com.apple.quicktime-movie";
assetExport.outputURL = [NSURL fileURLWithPath:outFilePath];

[assetExport exportAsynchronouslyWithCompletionHandler:
 ^(void ) {
     switch (assetExport.status)
     {
         case AVAssetExportSessionStatusCompleted:
             //                export complete
             NSLog(@"Export Complete");
             [self performSelectorOnMainThread:@selector(creatingVideoDone:)
                                    withObject:outFilePath waitUntilDone:NO];
             [assetExport release];
             break;
         case AVAssetExportSessionStatusFailed:
             NSLog(@"Export Failed");
             NSLog(@"ExportSessionError: %@", [assetExport.error localizedDescription]);
             // Set delegate to move to view
             if ( mDelegate!= nil && [mDelegate respondsToSelector:@selector(errorAlert:)])
             {
                 [self performSelectorOnMainThread:@selector(errorOccured:)
                                        withObject:[assetExport.error
                                                    localizedDescription]
                                     waitUntilDone:NO];
             }
             [assetExport release];
             break;
         case AVAssetExportSessionStatusCancelled:
             NSLog(@"Export Failed");
             NSLog(@"ExportSessionError: %@", [assetExport.error localizedDescription]);
             // Set delegate to move to view
             if ( mDelegate!= nil && [mDelegate respondsToSelector:@selector(errorAlert:)])
             {
                 [self performSelectorOnMainThread:@selector(errorOccured:)
                                        withObject:[assetExport.error
                                                    localizedDescription]
                                     waitUntilDone:NO];
             }
             [assetExport release];
             break;
     }
 }];
NSAutoreleasePool*pool=[[NSAutoreleasePool alloc]init];
n错误*错误=nil;
AVMutableComposition*composition=[AVMutableComposition];
NSURL*url=[NSURL fileURLWithPath:filePath];
AVURLAsset*videoAsset=[AVURLAsset UrlAssetTwithUrl:url选项:无];
AVAssetTrack*videoAssetTrack=[[videoAsset tracksWithMediaType:AVMediaTypeVideo]对象索引:0];
AVMutableCompositionTrack*compositionVideoTrack=[composition addMutableTrackWithMediaType:AVMediaTypeVideo
preferredTrackID:kCompersistentTrackID_无效];
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,videoAsset.duration)of Track:videoAssetTrack atTime:kCMTimeZero错误:&错误];
CMTime audioStartTime=kCMTimeZero;
对于(NSInteger i=0;i<[marraAudioFileNames计数];i++)
{
NSString*audioFileName=nil;
NSString*docsDir=nil;
if([mArrAudioFileNames对象索引:i]!=[NSNull]){
audioFileName=[mArrAudioFileNames对象索引:i];
docsDir=[[self-dataFolderPathForAudio]stringByAppendingPathComponent:audioFileName];
}否则{
//audioFileName=@;
docsDir=[[NSBundle mainBundle]pathForResource:@“mp3”类型的“sample”;
}
//NSString*docsDir=[[self-dataFolderPathForAudio]stringByAppendingPathComponent:audioFileName];
AVURLAsset*urlAsset=[AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:docsDir]选项:nil];
AVAssetTrack*audioAssetTrack=[[urlAsset tracksWithMediaType:AVMediaTypeAudio]对象索引:0];
AVMutableCompositionTrack*compositionAudioTrack=[CompositionAddMutableTrackWithMediaType:AVMediaTypeAudio
preferredTrackID:kCompersistentTrackID_无效];
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,urlAsset.duration)of Track:audioAssetTrack atTime:audioStartTime错误:&error];
Float64 duration=CMTimeGetSeconds(urlAsset.duration);
audioStartTime=CMTimeAdd(audioStartTime,CMTimeMake((int)((持续时间*kRecordingFPS)+0.5),kRecordingFPS));
}
AVAssetExportSession*assetExport=[[AVAssetExportSession alloc]initWithAsset:composition预设名称:AVAssetExportPresetMediumQuality];
//assetExport.videoComposition=合成视频跟踪;
assetExport.outputFileType=AVFileTypeQuickTimeMovie;//@“com.apple.quicktime电影”;
assetExport.outputURL=[NSURL fileURLWithPath:outFilePath];
[assetExport exportAsynchronouslyWithCompletionHandler:
^(无效){
交换机(assetExport.status)
{
案例AvassetExportSessionStatus已完成:
//出口完成
NSLog(“导出完成”);
[self-performSelectorOnMainThread:@selector(creatingVideoDone:)
withObject:outFilePath waitUntilDone:NO];
[港口放行];
打破
案例AvassetExportSessionStatus失败:
NSLog(@“导出失败”);
NSLog(@“ExportSessionError:%@,[assetExport.error localizedDescription]);
//将委托设置为移动到视图
if(mDelegate!=nil&&[mDelegate respondsToSelector:@selector(errorAlert:)]))
{
[self-performSelectorOnMainThread:@selector(发生错误:)
withObject:[assetExport.error
本地化描述]
waitUntilDone:没有];
}
[港口放行];
打破
案例AvassetExportSessionStatus取消:
NSLog(@“导出失败”);
NSLog(@“ExportSessionError:%@,[assetExport.error localizedDescription]);
//将委托设置为移动到视图
if(mDelegate!=nil&&[mDelegate respondsToSelector:@selector(errorAlert:)]))
{
[self-performSelectorOnMainThread:@selector(发生错误:)
withObject:[assetExport.error
本地化描述]
waitUntilDone:没有];
}
[港口放行];
打破
}
}];
感谢您的帮助


谢谢。

您是否尝试过其他值而不是AvassetExportPresetMediumQuality是的。我还使用了AVAssetExportPresetHighestQuality值。它确实增加了最终输出文件的大小(数据),但分辨率不同。是否还有指定宽度和高度的方法?@subhashAmale找到了用宽度和高度调整大小的答案?