天天看點

如何使用開源工具集合 FFmpeg 減小視訊大小

如何使用開源工具集合 FFmpeg 減小視訊大小

FFmpeg 是一個用于處理多媒體檔案的開源工具集合。FFmpeg 包含一組共享的多媒體庫,例如 libavcodec、libavutil 和 libavformat。允許你轉換視訊和音頻檔案、執行流和調整視訊檔案的大小。

如何安裝FFmpeg

本文介紹如何在Centos8中安裝ffmpeg,FFmpeg 在預設的CentOS 8 源倉庫中沒有提供,可以從第三方軟體源中安裝:

[[email protected] ~]# rpm -ivh https://mirrors.ustc.edu.cn/rpmfusion/free/el/rpmfusion-free-release-8.noarch.rpm
[[email protected] ~]# yum config-manager --enable PowerTools
[[email protected] ~]# yum -y install SDL2
[[email protected] ~]# yum -y install ffmpeg
           

 安裝完成之後可以檢視一下ffmpeg的版本:

[[email protected] ~]# ffmpeg -version
[[email protected] ~]# ffmpeg -version
ffmpeg version 4.2.4 Copyright (c) 2000-2020 the FFmpeg developers
built with gcc 8 (GCC)
configuration: --prefix=/usr --bindir=/usr/bin --datadir=/usr/share/ffmpeg --docdir=/usr/share/doc/ffmpeg --incdir=/usr/include/ffmpeg --libdir=/usr/lib64 --mandir=/usr/share/man --arch=x86_64 --optflags='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection' --extra-ldflags='-Wl,-z,relro -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld ' --extra-cflags=' ' --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvo-amrwbenc --enable-version3 --enable-bzlib --disable-crystalhd --enable-fontconfig --enable-frei0r --enable-gcrypt --enable-gnutls --enable-ladspa --enable-libaom --enable-libdav1d --enable-libass --enable-libbluray --enable-libcdio --enable-libdrm --enable-libjack --enable-libfreetype --enable-libfribidi --enable-libgsm --enable-libmp3lame --enable-nvenc --enable-openal --enable-opencl --enable-opengl --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-librsvg --enable-libsrt --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libvorbis --enable-libv4l2 --enable-libvidstab --enable-libvmaf --enable-version3 --enable-vapoursynth --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-libzimg --enable-libzvbi --enable-avfilter --enable-avresample --enable-libmodplug --enable-postproc --enable-pthreads --disable-static --enable-shared --enable-gpl --disable-debug --disable-stripping --shlibdir=/usr/lib64 --enable-libmfx --enable-runtime-cpudetect
libavutil      56. 31.100 / 56. 31.100
libavcodec     58. 54.100 / 58. 54.100
libavformat    58. 29.100 / 58. 29.100
libavdevice    58.  8.100 / 58.  8.100
libavfilter     7. 57.100 /  7. 57.100
libavresample   4.  0.  0 /  4.  0.  0
libswscale      5.  5.100 /  5.  5.100
libswresample   3.  5.100 /  3.  5.100
libpostproc    55.  5.100 / 55.  5.100
           
如何使用開源工具集合 FFmpeg 減小視訊大小

FFmpeg基本用法

轉換多媒體檔案時,選擇正确的編解碼器、容器和格式可能非常具有挑戰性。但是,使用 FFmpeg,可以堅持預設設定并期待出色的結果。要使用預設 FFmpeg 設定轉換檔案,請使用以下指令:

[[email protected] ~]# ffmpeg -i video.mp4 out_video.mp4
           
如何使用開源工具集合 FFmpeg 減小視訊大小

 可以看到下圖中,輸出的視訊變小了許多:

如何使用開源工具集合 FFmpeg 減小視訊大小

上面的指令會将檔案從指定格式轉換為輸出格式。但是,要有效地壓縮檔案,我們需要使用編解碼器、比特率、容器和 fps。

如何使用開源工具集合 FFmpeg 減小視訊大小

【CSDN背景扣1領取,免費分享】資料包括《Andoird音視訊開發必備手冊+音視訊學習視訊+學習文檔資料包+大廠面試真題+2022最新學習路線圖》等, 

如何使用 FFmpeg 減少視訊大小

沒有通用的方法來減小視訊檔案的大小,因為各種檔案類型的建立并不相同。在本教程中,我們将使用 x265 編解碼器。x265 編解碼器,它是一個免費的庫,用于視訊編碼為 H.254/MPEG-H HEVC 的壓縮格式。CRF使用 0 到 51 之間的數字。恒定速率因子(CRF)是 x264 和 x265 編碼的預設品質設定。值越高,壓縮率越高,值越高這可能會導緻品質損失。下面我們将一個274M的視訊檔案進行壓縮:

[[email protected] ~]# ffmpeg -i video.mp4 -vcodec libx265 -crf 25 compress_video.mp4
           
如何使用開源工具集合 FFmpeg 減小視訊大小

總 結

雖然使用自定義壓縮選項可以産生很好的結果,但在某些情況下使用預設的 FFmpeg 選項可能會更好。

繼續閱讀