java - 使用库或算法压缩视频大小的最快方法

标签 java python objective-c macos ffmpeg

我正在尝试将高质量视频压缩成更小的尺寸,并且我能够使用以下 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。我正在使用 native AVFoundation 库来压缩视频以减小其大小。

我需要像 Apple Compressor 应用程序一样非常快速地压缩视频大小,它仅在 30 秒内压缩 500MB 文件...

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

我也为此使用了 FFMPEG 库,但速度也很慢,我发现该库不再有用。

我还尝试使用其他语言(如 java、python)找到解决方案。但没有找到任何解决方案。

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

谢谢...

最佳答案

这是一个文档,其中包含一些关于视频压缩的 RnD: https://drive.google.com/file/d/0BySTUFVtfzJmM3pYeVRiWmxSLVU/view

它还包含用于在 Objective-C 中快速压缩视频的示例代码。 它使用 AVAssetExportSession 来压缩视频。

关于java - 使用库或算法压缩视频大小的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38731535/

相关文章:

java - 如何在 ADT 中开始创建新应用程序时删除 fragment 布局?

python - 井号不显示 Jinja2(编码)

ios - 用 JWPlayer 通知替换 MPMoviePlayer 通知

objective-c - 如何将静态库嵌入到 iOS 框架中?

java - 为什么我不能将此 JSON 对象传递给另一个 Activity

java - 如何动态更改工具栏命令文本

java - 将 Java Web Start 应用程序转换为 Applet

python - 在两个数据之间保持相同的因式分解

python - 当按下 kivy 屏幕上的 recycleview 中的 viewclass 按钮时,如何移动到下一个屏幕?

iphone - 按取消按钮不会缩回