|
| 1 | + |
| 2 | +FFmpeg多媒体库支持的命令行调用分为三个模块:ffmpeg、ffprobe、ffplay。 |
| 3 | +其中ffmpeg命令行常用于音视频剪切、转码、滤镜、拼接、混音、截图等; |
| 4 | +ffprobe用于检测多媒体流格式;ffplay用于播放视频,包括本地与网络视频。 |
| 5 | +详情可查阅FFmpeg官方文档:ffmpeg Documentation。 |
| 6 | + |
| 7 | +# 一、ffmpeg命令行 |
| 8 | +## 1、命令行参数介绍 |
| 9 | +Print help / information / capabilities: |
| 10 | +--help topic show help |
| 11 | +-version show version |
| 12 | +-formats show available formats |
| 13 | +-muxers show available muxers |
| 14 | +-demuxers show available demuxers |
| 15 | +-devices show available devices |
| 16 | +-codecs show available codecs |
| 17 | +-decoders show available decoders |
| 18 | +-encoders show available encoders |
| 19 | +-bsfs show available bit stream filters |
| 20 | +-protocols show available protocols |
| 21 | +-filters show available filters |
| 22 | +-pix_fmts show available pixel formats |
| 23 | +-sample_fmts show available audio sample formats |
| 24 | +-hwaccels show available HW acceleration methods |
| 25 | + |
| 26 | +Global options (affect whole program instead of just one file: |
| 27 | +-v loglevel set logging level |
| 28 | +-y overwrite output files |
| 29 | +-n never overwrite output files |
| 30 | +-filter_threads number of non-complex filter threads |
| 31 | +-stats print progress report during encoding |
| 32 | +-vol volume change audio volume (256=normal) |
| 33 | + |
| 34 | +Per-file main options: |
| 35 | +-f fmt force format |
| 36 | +-c codec codec name |
| 37 | +-codec codec codec name |
| 38 | +-t duration record or transcode "duration" seconds of audio/video |
| 39 | +-to time_stop record or transcode stop time |
| 40 | +-fs limit_size set the limit file size in bytes |
| 41 | +-ss time_off set the start time offset |
| 42 | +-sseof time_off set the start time offset relative to EOF |
| 43 | +-frames number set the number of frames to output |
| 44 | +-filter filter_graph set stream filtergraph |
| 45 | +-discard discard |
| 46 | +-disposition disposition |
| 47 | + |
| 48 | +Video options: |
| 49 | +-vframes number set the number of video frames to output |
| 50 | +-r rate set frame rate (Hz value, fraction or abbreviation) |
| 51 | +-s size set frame size (WxH or abbreviation) |
| 52 | +-aspect aspect set aspect ratio (4:3, 16:9 or 1.3333, 1.7777) |
| 53 | +-bits_per_raw_sample number set the number of bits per raw sample |
| 54 | +-vn disable video |
| 55 | +-vcodec codec force video codec ('copy' to copy stream) |
| 56 | +-timecode hh:mm:ss[:;.]ff set initial TimeCode value. |
| 57 | +-vf filter_graph set video filters |
| 58 | +-ab bitrate audio bitrate (please use -b:a) |
| 59 | +-b bitrate video bitrate (please use -b:v) |
| 60 | + |
| 61 | +Audio options: |
| 62 | +-aframes number set the number of audio frames to output |
| 63 | +-aq quality set audio quality (codec-specific) |
| 64 | +-ar rate set audio sampling rate (in Hz) |
| 65 | +-ac channels set number of audio channels |
| 66 | +-an disable audio |
| 67 | +-acodec codec force audio codec ('copy' to copy stream) |
| 68 | +-vol volume change audio volume (256=normal) |
| 69 | +-af filter_graph set audio filters |
| 70 | + |
| 71 | +Subtitle options: |
| 72 | +-s size set frame size |
| 73 | +-sn disable subtitle |
| 74 | +-scodec codec force subtitle codec ('copy' to copy stream) |
| 75 | + |
| 76 | +命令行整体格式:以ffmpeg开头,参数之间用空格隔开,每个参数key以"-"开头,后面跟着参数value,输出文件放在命令行最后。 |
| 77 | + |
| 78 | +## 2、音频常见操作 |
| 79 | +### 2.1 音频转码 |
| 80 | +直接转码: |
| 81 | +ffmpeg -i input.mp3 output.m4a |
| 82 | +指定编码器、采样率、声道数转码: |
| 83 | +ffmpeg -i input.mp3 -acodec aac -ac 2 -ar 44100 output.m4a |
| 84 | + |
| 85 | +### 2.2 音频剪切 |
| 86 | +-ss 10指定从第10秒开始,-t 20代表剪切20秒 |
| 87 | +ffmpeg -i input.mp3 -ss 10 -t 20 cut.mp3 |
| 88 | + |
| 89 | +### 2.3 音频拼接 |
| 90 | +ffmpeg -i concat:"hello.mp3|world.mp3" -acodec copy -vn concat.mp3 |
| 91 | + |
| 92 | +### 2.4 音频混音 |
| 93 | +使用amix,参数inputs代表输入流个数,duration有longest、shortest、first三种模式,weights设置每条流音量权重占比: |
| 94 | +ffmpeg -i hello.mp3 -i world.mp3 -filter_complex amix=inputs=2:duration=first -vn mix.mp3 |
| 95 | +使用amerge,合并成多声道输出: |
| 96 | +ffmpeg -i hello.mp3 -i world.mp3 -filter_complex [0:a][1:a]amerge=inputs=2[aout] -map [aout] merge.mp3 |
| 97 | + |
| 98 | +### 2.5 空灵音效 |
| 99 | +aecho接收4个参数:in_gain (0, 1];out_gain (0, 1];delays (0 - 90000];decays (0 - 1.0] |
| 100 | +ffmpeg -i input.mp3 -af aecho=0.8:0.8:1000:0.5 echo.mp3 |
| 101 | + |
| 102 | +### 2.6 惊悚音效 |
| 103 | +tremolo接收2个参数:frequency [0.1, 20000.0];depth (0, 1] |
| 104 | +ffmpeg -i input.mp3 -af tremolo=5:0.9 tremolo.mp3 |
| 105 | + |
| 106 | +### 2.7 搞笑音效 |
| 107 | +搞笑音效通过调节音速实现,使用atempo: |
| 108 | +ffmpeg -i input.mp3 -filter_complex atempo=0.5 atempo.mp3 |
| 109 | + |
| 110 | +### 2.8 静音检测 |
| 111 | +ffmpeg -i input.mp3 -af silencedetect=noise=0.0001 -f null - |
| 112 | + |
| 113 | +### 2.9 修改音量 |
| 114 | +ffmpeg -i input.mp3 -af volume=0.5 volume.mp3 |
| 115 | + |
| 116 | +### 2.10 抽取音频 |
| 117 | +从视频抽取音频,也就是disable视频流、拷贝音频流: |
| 118 | +ffmpeg -i input.mp4 -acodec copy -vn out.mp3 |
| 119 | + |
| 120 | +## 3、视频常见操作 |
| 121 | +### 3.1 视频剪切 |
| 122 | +基本剪切,指定起始时间、剪切时长: |
| 123 | +ffmpeg -i input.mp4 -ss 10 -t 20 -codec copy cut.mp4 |
| 124 | +精确剪切,包含多音轨,-map 0代表所有track流进行剪切,-accurate_seek代表精确seek: |
| 125 | +ffmpeg -i input.mp4 -ss 10 -accurate_seek -t 20 -map 0 -codec copy cut.mp4 |
| 126 | + |
| 127 | +### 3.2 视频转码 |
| 128 | +使用-vcodec指定视频编码,-acodec指定音频编码,-s 640x480指定视频分辨率, |
| 129 | +-b 200k指定码率,-r 20指定帧率,这样达到视频压缩效果: |
| 130 | +ffmpeg -i input.mp4 -vcodec libx264 -acodec aac -s 640x480 -b 200k -r 20 transcode.mp4 |
| 131 | + |
| 132 | +### 3.3 视频截图 |
| 133 | +使用-vframes指定截图数量,-ss指定起始时间放在-i前面,这样保证先seek到指定位置再截图。 |
| 134 | +如果是先-i指定输入文件再-ss,是从时间0开始解码,直到指定时间再截图,这样效率太低: |
| 135 | +ffmpeg -ss 10 -i input.mp4 -f image2 -vframes 1 -an screenshot.jpg |
| 136 | + |
| 137 | +### 3.4 图片水印 |
| 138 | +使用-filter_complex指定位置overlay=x:y,如下所示: |
| 139 | +ffmpeg -i input.mp4 -i logo.png -filter_complex overlay=10:20 pic_watermark.mp4 |
| 140 | +如果要灵活设置左上角、右上角、左下角、右下角位置,可以使用case语句,其中main_w代表视频宽度, |
| 141 | +main_h代表视频高度,overlay_w代表水印宽度,overlay_h代表水印高度: |
| 142 | + |
| 143 | +public static String obtainOverlay(int offsetX, int offsetY, int location) { |
| 144 | + switch (location) { |
| 145 | + case 2: |
| 146 | + return "overlay='(main_w-overlay_w)-" + offsetX + ":" + offsetY + "'"; |
| 147 | + case 3: |
| 148 | + return "overlay='" + offsetX + ":(main_h-overlay_h)-" + offsetY + "'"; |
| 149 | + case 4: |
| 150 | + return "overlay='(main_w-overlay_w)-" + offsetX + ":(main_h-overlay_h)-" + offsetY + "'"; |
| 151 | + case 1: |
| 152 | + default: |
| 153 | + return "overlay=" + offsetX + ":" + offsetY; |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +public static String[] addWaterMarkImg(String inputPath, String imgPath, int location, |
| 158 | + int offsetXY, String outputPath) { |
| 159 | + String overlay = obtainOverlay(offsetXY, offsetXY, location); |
| 160 | + String waterMarkCmd = "ffmpeg -i %s -i %s -filter_complex %s -preset:v superfast %s"; |
| 161 | + waterMarkCmd = String.format(waterMarkCmd, inputPath, imgPath, overlay, outputPath); |
| 162 | + return waterMarkCmd.split(" "); |
| 163 | +} |
| 164 | + |
| 165 | +### 3.5 GIF水印 |
| 166 | +使用-ignore_loop 0代表GIF循环显示,其他操作与图片水印一致: |
| 167 | +ffmpeg -i input.mp4 -ignore_loop 0 -i logo.gif -filter_complex overlay=10:20 gif_mark.mp4 |
| 168 | + |
| 169 | +### 3.6 去除水印 |
| 170 | +使用delogo命令,然后指定水印位置: |
| 171 | +ffmpeg -i input.mp4 -filter_complex delogo=x=10:y=20:w=90:h=30 delogo.mp4 |
| 172 | + |
| 173 | +### 3.7 视频拼接 |
| 174 | +视频拼接分为垂直拼接、水平拼接,还有前后拼接,这里主要介绍垂直与水平拼接,其中使用hstack做水平拼接,vstack做垂直拼接: |
| 175 | +ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex hstack out.mp4 |
| 176 | + |
| 177 | +### 3.8 视频翻转 |
| 178 | +使用reverse实现视频翻转,但是处理比较耗时: |
| 179 | +ffmpeg -i input.mp4 -vf reverse -an output.mp4 |
| 180 | + |
| 181 | +### 3.9 视频降噪 |
| 182 | +使用-nr代表noise reduction,进行视频降噪: |
| 183 | +ffmpeg -i in.mp4 -nr 500 out.mp4 |
| 184 | + |
| 185 | +### 3.10 视频抽帧 |
| 186 | +ffmpeg -ss 20 -accurate_seek -t 10 -i input.mp4 -an -r 5 %3d.jpg |
| 187 | + |
| 188 | +### 3.11 播放速度 |
| 189 | +使用setpts设置视频速度,atempo设置音频速度: |
| 190 | +ffmpeg -i in.mp4 -filter_complex [0:v]setpts=%.2f*PTS[v];[0:a]atempo=%.2f[a] -map [v] -map [a] out.mp4 |
| 191 | + |
| 192 | +### 3.12 插入封面 |
| 193 | +其中-disposition:v:1 attached_pic代表封面: |
| 194 | +ffmpeg -i in.mp4 -i photo.png -map 0 -map 1 -c copy -c:v:1 png -disposition:v:1 attached_pic out.mp4 |
| 195 | + |
| 196 | +### 3.13 旋转视频 |
| 197 | +ffmpeg -i in.mp4 -c copy -metadata:s:v:0 rotate=90 out.mp4 |
| 198 | + |
| 199 | +# 二、ffplay命令行 |
| 200 | +ffplay主要用于播放视频,也可以播放网络流,示例如下: |
| 201 | +ffplay -i beyond.mp4 |
| 202 | + |
| 203 | +# 三、ffprobe命令行 |
| 204 | +ffprobe主要用于检测多媒体信息,包括时长、视频分辨率、帧率、音频采样率、声道数、每个stream流信息等等。 |
| 205 | +支持显示形式包括:json和xml,例如-print_format json |
| 206 | +ffprobe -i input.mp4 -show_streams -show_format -print_format json |
| 207 | + |
| 208 | +如果要显示每帧数据信息,使用-show_frames可以显示pts、packet_size、duration、frame_type等信息: |
| 209 | +ffprobe -i input.mp4 -show_frames |
| 210 | + |
| 211 | +如果只要显示视频流,使用-select_streams v。其中v代表video,a代表audio,s代表subtitle。 |
| 212 | +ffprobe -i input.mp4 -show_frames -select_streams v |
0 commit comments