最近因爲業務需要,要對上傳的圖片 和 視屏做壓縮處理,我們都知道,凡事涉及到圖形圖像方面的,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