使用库或Alg压缩视频大小的最快方法

2024-09-27 23:23:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将高质量的视频压缩成更小的大小,我可以使用以下objective-c代码来缩小压缩后的视频大小:

 - (BOOL)convertMovieToMP4:(NSString ) originalMovPath andStoragePath:(NSString ) compMovPath
        {
            NSURL *tmpSourceUrl = [NSURL fileURLWithPath:originalMovPath];

            compMovPath = [compMovPath stringByReplacingOccurrencesOfString:[compMovPath pathExtension] withString:@"mp4"];
            NSURL *tmpDestUrl = [NSURL fileURLWithPath:compMovPath];

            AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:tmpSourceUrl options:nil];
            AVMutableComposition* mixComposition = [AVMutableComposition composition];

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

            AVAssetTrack *clipVideoTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
            [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
                                           ofTrack:clipVideoTrack
                                            atTime:kCMTimeZero error:nil];

            [compositionVideoTrack setPreferredTransform:[[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] preferredTransform]];

            CGSize videoSize = [[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] naturalSize];

            CATextLayer *titleLayer = [CATextLayer layer];
            titleLayer.string = @"Ojatro";
            titleLayer.font = (_bridge CFTypeRef Nullable)(@"Helvetica");
            titleLayer.fontSize = videoSize.height / 8;
            titleLayer.shadowOpacity = 0.2;
            titleLayer.alignmentMode = kCAAlignmentCenter;
            titleLayer.bounds = CGRectMake(0, 0, videoSize.width, videoSize.height / 6);
            titleLayer.position=CGPointMake(videoSize.width/2, videoSize.height/2);

            CALayer *parentLayer = [CALayer layer];
            CALayer *videoLayer = [CALayer layer];
            parentLayer.frame = CGRectMake(0, 0, videoSize.width, videoSize.height);
            videoLayer.frame = CGRectMake(0, 0, videoSize.width, videoSize.height);
            [parentLayer addSublayer:videoLayer];
            [parentLayer addSublayer:titleLayer];

            AVMutableVideoComposition* videoComp = [AVMutableVideoComposition videoComposition];
            videoComp.renderSize = videoSize;
            videoComp.frameDuration = CMTimeMake(1, 30);
            videoComp.animationTool = [AVVideoCompositionCoreAnimationTool      videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];

            AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
            instruction.timeRange = CMTimeRangeMake(kCMTimeZero, [mixComposition duration]);

            AVAssetTrack *videoTrack = [[mixComposition tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

            AVMutableVideoCompositionLayerInstruction* layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];

            instruction.layerInstructions = [NSArray arrayWithObject:layerInstruction];
            videoComp.instructions = [NSArray arrayWithObject: instruction];

            AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetMediumQuality];//AVAssetExportPresetPasst
            _assetExport.videoComposition = videoComp;
            //NSString* videoName = @"mynewwatermarkedvideo.mov";
            NSString *tmpDirPath = [compMovPath stringByReplacingOccurrencesOfString:[compMovPath lastPathComponent] withString:@""];
            if ([Utility makeDirectoryAtPath:tmpDirPath])
            {
                NSLog(@"Directory Created");
            }
            //exportPath=[exportPath stringByappendingString:videoName];
            NSURL    *exportUrl = tmpDestUrl;

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

            _assetExport.outputURL = exportUrl;
            _assetExport.shouldOptimizeForNetworkUse = YES;
            _assetExport.outputFileType = AVFileTypeMPEG4;

            //[strRecordedFilename setString: exportPath];

            [_assetExport exportAsynchronouslyWithCompletionHandler:
             ^(void ) {
                 switch (_assetExport.status)
                 {
                     case AVAssetExportSessionStatusUnknown:
                         NSLog(@"Export Status Unknown");

                         break;
                     case AVAssetExportSessionStatusWaiting:
                         NSLog(@"Export Waiting");

                         break;
                     case AVAssetExportSessionStatusExporting:
                         NSLog(@"Export Status");

                         break;
                     case AVAssetExportSessionStatusCompleted:
                         NSLog(@"Export Completed");
                         totalFilesCopied++;
                         [self startProgressBar];

                         break;
                     case AVAssetExportSessionStatusFailed:
                         NSLog(@"Export failed");

                         break;
                     case AVAssetExportSessionStatusCancelled:
                         NSLog(@"Export canceled");

                         break;
                 }
             }
             ];
            return NO;
        }

但我的主要问题是,当我压缩500MB的视频(即平均视频)文件时,大约需要20到30分钟。它将视频大小减少到大约130MB。使用avm压缩其原生视频库的大小。在

我需要压缩的视频大小非常快,就像苹果压缩机应用程序,它压缩500MB文件在30秒内只。。。在

https://itunes.apple.com/en/app/compressor/id424390742?mt=12

我也用过FFMPEG库,但这也太慢了,我没有发现这个库有用了。在

我也尝试过使用其他语言如java、python来找到解决方案。但没有找到任何解决办法。在

如果任何人有这个特定问题的解决方案,或者有一些库(即付费库或开源库)可以用最少1分钟的时间完成压缩。。。请与我分享。或者其他一些代码,可以克服压缩时间问题,从20-30分钟到至少1分钟。在

谢谢。。。在


Tags: 视频exportcaseheightbreaknslognsurltitlelayer

热门问题