文章目錄
- ffmpeg安裝、配置
- java運行報錯 Cannot run program "ffmpeg"
- ffmpeg命令
- mp4轉為swf示例
- ### ffmpeg -i input.mkv -b:v 600 -c:v libx264 -vf scale=1920:1080 -crf 10 -ar 48000 -r 24 output.swf
- mkv轉為swf示例
- 其他
- 文檔
- 命令參數簡介
需要將mp4轉換為swf,網上有很多軟件,不是收費,就是功能不全,要不就是分辨率比例不滿足要求。突然想到實在不行就自己開發個,誰讓自己是程序員呢。
ffmpeg安裝、配置
不是只寫程序就行,需要先安裝ffpmeg。
下載地址:
https://www.gyan.dev/ffmpeg/builds/#release-builds # 下載zip包就行
然后配置環境變量,在path中添加即可。
ffmpeg.exe所在的目錄就是要添加到path中的路徑。如:
D:\Program Files\ffmpeg\ffmpeg-7.0.1-essentials_build\bin
打開cmd,輸入ffmpeg -h
命令試下是否可以了。
注:git bash命令行最好也添加下。
java運行報錯 Cannot run program “ffmpeg”
Cannot run program “ffmpeg -i d:\swf\input.mp4 -vcodec libx264 -f flv d:\swf\output.swf”: CreateProcess error=2, 系統找不到指定的文件
解決方案:
1、ffmpeg已安裝,且環境變量已配置。
2、git bash的安裝路徑下,也添加bin下的3個exe。
但還是報錯,這就怪了。
ffmpeg命令
初衷原本是想用java開發的,后來發現直接用命令就可以,那應該比java更方便。
這里特別強調下,ffmpeg是個非常強大的命令,里面知識特別多,如果連擴展也算上,如:ffmpeg-all,ffplay。那么知識就更多了,要花太多的時間。
想要用好這個命令,對視頻音頻各種編碼還需要有一定認知,一般人都不太懂這個。
所以,我們會用幾個常用命令,能實現簡單轉換就夠了。
命令模板:
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
ffmpeg -version # 查看版本
ffmpeg -h # 幫助
以下表格內三種寫法是等價的:
音頻 | 視頻 | 字幕 |
---|---|---|
-codec:a | -codec:v | -codec:s |
-c:a | -c:v | -c:s |
-acodec | -vcodec | -scodec |
①、主要命令選項
-f fmt (input/output)
:指定輸入或者輸出文件格式(封裝格式,視頻容器)。常規可省略,而使用依據擴展名(文件的前幾百 K 的內容,智能分析)的自動指定,但一些選項需要強制明確設定。
-i filename(input)
:指定輸入文件。
-y(global)
:默認自動覆蓋輸出文件,而不再詢問確認。
-n( global)
:不覆蓋輸出文件,如果輸出文件已經存在則立即退出。
-t duration( input/output)
:限制輸入/輸出的時間。如果是在 -i 前面,就是限定從輸入中讀取多少時間的數據;如果是用于限定輸出文件,則表示寫入多少時間數據后就停止。duration 可以是以秒為單位的數值或者 hh:mm:ss.xxx 格式的時間值。注意 -to 和 -t 是互斥的,-t 有更高優先級。
-to position (output)
:只寫入 position 時間后就停止,position 可以是以秒為單位的數值或者 hh:mm:ss.xxx 格式的時間值。注意 -to 和 -t 是互斥的,-t 有更高優先級。
-ss position (input/output)
:當在 -i 前,表示定位輸入文件到 position 指定的位置。注意可能一些格式是不支持精確定位的,所以 ffmpeg 可能是定位到最接近 position(在之前)的可定位點。position 可以是以秒為單位的數值或者 hh:mm:ss.xxx 格式的時間值。
-codec[:stream_specifier] codec (input/output,per-stream)
:為特定的文件選擇編/解碼模式,對于輸出文件就是編碼器, 對于輸入或者某個流就是解碼器。選項參數中 codec 是編解碼器的名字,或者是 copy(僅對輸出文件)則意味著流數據直接復制而不再編碼。
使用下面命令可以檢測 ffmepg 所支持的所有編碼器的格式
ffmpeg -encoders # 查看所支持的所有編碼器的格式,內容太多,不列出了,還是那句話ffmpeg是個比較專業的東西
mp4轉為swf示例
ffmpeg -i input.mp4 -vcodec copy -f flv output.swf
參數說明:
-i input.mp4 指定輸入文件。
-vcodec copy 表示復制視頻編解碼器數據。
-f flv 指定輸出格式為FLV,因為SWF通常通過FLV容器格式傳輸視頻。
### ffmpeg -i input.mkv -b:v 600 -c:v libx264 -vf scale=1920:1080 -crf 10 -ar 48000 -r 24 output.swf
報錯:
SWF muxer only supports VP6, FLV, Flash Screen Video, P
NG and MJPEG
mkv轉為swf示例
ffmpeg -i input.mkv -c:v flashvideo -c:a flashaudio -f flash output.swf
在這個命令中:
-i input.mkv 指定輸入文件。
-c:v flashvideo 指定視頻編碼器為flashvideo,這是SWF格式支持的視頻編碼器之一。
-c:a flashaudio 指定音頻編碼器為flashaudio,這是SWF格式支持的音頻編碼器之一。
-f flash 指定輸出格式為SWF。
output.swf 是輸出文件的名稱。請注意,確保你的FFmpeg版本支持你想要使用的編碼器。如果你的版本不支持這些編碼器,你可能需要使用其他編碼器或者升級你的FFmpeg。
其他
文檔
官網文檔:
https://ffmpeg.org/ffmpeg.html
這個是文檔詳細描述頁:
https://ffmpeg.org/documentation.html
比較不錯的文章:
FFmpeg常用命令行講解及實戰一
命令參數簡介
ffmpeg -h返回的內容:
ffmpeg -h
返回:
ffmpeg version 7.0.1-essentials_build-www.gyan.dev Copyright (c) 2000-2024 the FFmpeg developersbuilt with gcc 13.2.0 (Rev5, Built by MSYS2 project)configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-bzlib --enable-lzma --enable-zlib --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-sdl2 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libaom --enable-libopenjpeg --enable-libvpx --enable-mediafoundation --enable-libass --enable-libfreetype --enable-libfribidi --enable-libharfbuzz --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-dxva2 --enable-d3d11va --enable-d3d12va --enable-ffnvcodec --enable-libvpl --enable-nvdec --enable-nvenc --enable-vaapi --enable-libgme --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libtheora --enable-libvo-amrwbenc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-librubberbandlibavutil 59. 8.100 / 59. 8.100libavcodec 61. 3.100 / 61. 3.100libavformat 61. 1.100 / 61. 1.100libavdevice 61. 1.100 / 61. 1.100libavfilter 10. 1.100 / 10. 1.100libswscale 8. 1.100 / 8. 1.100libswresample 5. 1.100 / 5. 1.100libpostproc 58. 1.100 / 58. 1.100
Universal media converter
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...Getting help:-h -- print basic options-h long -- print more options-h full -- print all options (including all format and codec specific options, very long)-h type=name -- print all options for the named decoder/encoder/demuxer/muxer/filter/bsf/protocolSee man ffmpeg for detailed description of the options.Per-stream options can be followed by :<stream_spec> to apply that option to specific streams only. <stream_spec> can be a stream index, or v/a/s for video/audio/subtitle (see manual for full syntax).Print help / information / capabilities:
-L show license
-h <topic> show help
-version show version
-muxers show available muxers
-demuxers show available demuxers
-devices show available devices
-decoders show available decoders
-encoders show available encoders
-filters show available filters
-pix_fmts show available pixel formats
-layouts show standard channel layouts
-sample_fmts show available audio sample formatsGlobal options (affect whole program instead of just one file):
-v <loglevel> set logging level
-y overwrite output files
-n never overwrite output files
-stats print progress report during encodingPer-file options (input and output):
-f <fmt> force container format (auto-detected otherwise)
-t <duration> stop transcoding after specified duration
-to <time_stop> stop transcoding after specified time is reached
-ss <time_off> start transcoding at specified timePer-file options (output-only):
-metadata[:<spec>] <key=value> add metadataPer-stream options:
-c[:<stream_spec>] <codec> select encoder/decoder ('copy' to copy stream without reencoding)
-filter[:<stream_spec>] <filter_graph> apply specified filters to audio/videoVideo options:
-r[:<stream_spec>] <rate> override input framerate/convert to given output framerate (Hz value, fraction or abbreviation)
-aspect[:<stream_spec>] <aspect> set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)
-vn disable video
-vcodec <codec> alias for -c:v (select encoder/decoder for video streams)
-vf <filter_graph> alias for -filter:v (apply filters to video streams)
-b <bitrate> video bitrate (please use -b:v)Audio options:
-aq <quality> set audio quality (codec-specific)
-ar[:<stream_spec>] <rate> set audio sampling rate (in Hz)
-ac[:<stream_spec>] <channels> set number of audio channels
-an disable audio
-acodec <codec> alias for -c:a (select encoder/decoder for audio streams)
-ab <bitrate> alias for -b:a (select bitrate for audio streams)
-af <filter_graph> alias for -filter:a (apply filters to audio streams)Subtitle options:
-sn disable subtitle
-scodec <codec> alias for -c:s (select encoder/decoder for subtitle streams)