android ios使用ffmpeg 你还在自己编译么

最近因为业务需要,要对上传的图片 和 视屏做压缩处理,我们都知道,凡事涉及到图形图像方面的,ffmpeg肯定是首选的,但是对于移动端ios ,android 都需要自己去编译,并且包的大小不是很好的控制,因为大多人如果不是做和ffmpeg 相关的的工作,连他如何顺利的编译都都搞不懂,更别提如何选择性的去编译包了.

       给大家推荐一个很好用的ffmpeg 库,mobile-ffmpeg   

     githup :https://github.com/tanersener/mobile-ffmpeg

     支持android 的远程依赖 及ios pod,只要一行代码就集成完毕,并且可以根据自己的业务需求选择使用那个依赖,即满足了业务需求,又不会造成多余无用的库在项目中增加体积.

 

  min min-gpl https https-gpl audio video full full-gpl
external libraries - vid.stab
x264
x265
xvidcore
gmp
gnutls
gmp
gnutls
vid.stab
x264
x265
xvidcore
lame
libilbc
libvorbis
opencore-amr
opus
shine
soxr
speex
twolame
wavpack
fontconfig
freetype
fribidi
kvazaar
libaom
libass
libiconv
libtheora
libvpx
libwebp
snappy
fontconfig
freetype
fribidi
gmp
gnutls
kvazaar
lame
libaom
libass
libiconv
libilbc
libtheora
libvorbis
libvpx
libwebp
libxml2
opencore-amr
opus
shine
snappy
soxr
speex
twolame
wavpack
fontconfig
freetype
fribidi
gmp
gnutls
kvazaar
lame
libaom
libass
libiconv
libilbc
libtheora
libvorbis
libvpx
libwebp
libxml2
opencore-amr
opus
shine
snappy
soxr
speex
twolame
vid.stab
wavpack
x264
x265
xvidcore
android system libraries zlib
MediaCodec
ios system libraries zlib
AudioToolbox
AVFoundation
CoreImage
iconv
VideoToolbox
bzip2
tvos system libraries zlib
AudioToolbox
CoreImage
iconv
VideoToolbox
bzip2

 

 

这里有八种方式供我们选择,可根据业务自己选择,我的项目只用到,图片压缩,视频裁剪和视频压缩。min-gpl 已经能满足我的需求了,因为要用到x264 .

  android 依赖方式:

com.arthenica:mobile-ffmpeg-min-gpl:4.3.2

使用:

import com.arthenica.mobileffmpeg.Config;
import com.arthenica.mobileffmpeg.FFmpeg;

int rc = FFmpeg.execute("-i file1.mp4 -c:v mpeg4 file2.mp4");

if (rc == RETURN_CODE_SUCCESS) {
    Log.i(Config.TAG, "Command execution completed successfully.");
} else if (rc == RETURN_CODE_CANCEL) {
    Log.i(Config.TAG, "Command execution cancelled by user.");
} else {
    Log.i(Config.TAG, String.format("Command execution failed with rc=%d and the output below.", rc));
    Config.printLastCommandOutput(Log.INFO);
}

 

  ios                      :

pod 'mobile-ffmpeg-min-gpl', '~> 4.3.2'

 使用

#import <mobileffmpeg/MobileFFmpegConfig.h>
#import <mobileffmpeg/MobileFFmpeg.h>

int rc = [MobileFFmpeg execute: @"-i file1.mp4 -c:v mpeg4 file2.mp4"];

if (rc == RETURN_CODE_SUCCESS) {
    NSLog(@"Command execution completed successfully.\n");
} else if (rc == RETURN_CODE_CANCEL) {
    NSLog(@"Command execution cancelled by user.\n");
} else {
    NSLog(@"Command execution failed with rc=%d and output=%@.\n", rc, [MobileFFmpegConfig getLastCommandOutput]);
}

 

当然你怕以后扩展改动麻烦可以直接用全量包  full-gpl

com.arthenica:mobile-ffmpeg-full-gpl:4.3.2

pod 'mobile-ffmpeg-full-gpl:', '~> 4.3.2'

 

最后记录几个图形图像ffmpeg 命令

压缩图片:

 ffmpeg -i " + inputPath + "  -s " + width + "x" + height + " -q:v 4 -f image2 -y " + outPath

-i :输入图片路径

-s:输出图片宽高

-q:v 4 -f image2 : 输出图片格式 像素质量是4

-y: 强制输出覆盖 会覆盖掉之前同名文件

视频裁剪:

ffmpeg -i input.mp4 -ss 开始时间 -t 持续时间 -vcodec copy -acodec copy -preset superfast outputFilePath.mp4

-ss:开始裁剪时间

-t:裁剪时间

视频压缩:

ffmpeg -i test.mp4 -r 10 -b:a 32k -c:v libx264 -vf scale=-2:960 test1_2.mp4

-r:视频帧率

-b:a:音频码率

-c:v : 用于控制平均码率

libx264 :使用x264 压缩

-vf scale=-2:960 :按照比例缩放,这里是高度固定960 ,宽度按比例缩放

最后将下调试方式,可以先在pc 上安装ffmpeg 通过命令行 执行 ,这样比较便捷,在移动端因为已经做了封装, ffmpeg 关键字不需要了,

可以这样:-i test.mp4 -r 10 -b:a 32k -c:v libx264 -vf scale=-2:960 test1_2.mp4

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章