java 视频压缩_java – 使用库或Algo压缩视频大小的最快方法

我正在尝试将高质量视频压缩到更小的尺寸,并且我能够使用以下

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 Library压缩视频以减小其大小.

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

我也使用了FFMPEG库,但这也很慢我没有发现该库更有用.

我也尝试使用其他语言,如java,python来找到解决方案.但未发现任何解决方案被发现.

如果有人有这个特定问题的解决方案,或者有一些库(即付费库或开源库)可以用更少的时间进行压缩至少1分钟…请与我分享.或者其他一些代码可以解决压缩时间问题,从20到30分钟到至少1分钟.

谢谢…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值