Some basic usage of ffmpeg

Source: Some basic usage of ffmpeg | Wang Shitou's blog (iwangsen.com)

Audio and video operations can hardly bypass ffmpeg, because most of the audio and video playback software and transcoding software on the market have their cores.

It is very powerful, you can successfully complete the work you want with a few commands

Let me summarize some frequently used commands

First enter the ffmpeg directory in the cmd window

The easiest video format conversion

1
2
3
4
5
6
7
8
9
10
11
12
13
ffmpeg -i D:\Media\IMG_0873.MOV -c copy  D:\Media\output.avi

意思是,把D:\Media目录下的源文件IMG_0873.MOV(视频:h.264,音频:aac)转换成output.avi(编码格式自动选择为:视频mpeg4,音频mp3),目标文件仍然保存到D:\Media目录下。

问题来了:我想自己指定编码格式,怎么办呢?一种方法是,通过目标文件的扩展名(.flv、.mpg、.mp4、.wmv等)来控制,比如:

ffmpeg -i D:\Media\IMG_0873.MOV D:\Media\output2.flv   只需要修改格式名称就OK,比如这个命令就转为了flv格式

如果是整个文件夹中的所有flv文件需要批量转成mp4,那么使用以下命令:

for %i in (*.flv) do ffmpeg -i "%i" -c copy "%~ni.mp4"

注:不要更改以上的任何一个字符,新生成的mp4文件会自动拷贝原文件名。

I found that some of the videos I took with my mobile phone are upside down, and I want to rotate it 90 degrees clockwise. At this time, you can use the -vf parameter to add a filter, as follows:

1
2
3
ffmpeg -i D:\Media\IMG_0873.MOV -vf "rotate=90*PI/180" D:\Media\output3.avi

注:如果想逆时针旋转90度,90前面加个负号就可以了。

Video capture

1
2
3
ffmpeg -ss 2 -t 10 -i D:\Media\IMG_0873.MOV D:\Media\output4.avi

注:这种情况下,-ss和-t参数必须放在-i前面,表示是限定后面跟着的输入文件的。

Intercept by duration

1
ffmpeg -i demo.mp3 -ss 00:00:00 -t 00:00:05 -acodec copy out.mp3

 

Intercept according to the length of time and replace the audio in the video

1
ffmpeg -ss 00:00:00 -t 00:00:05 -i demo.mp4  -ss 00:00:00 -t 00:00:05  -i demo.mp3  -c:v copy -c:a aac -strict experimental -map 0:v:0 -map 1:a:0  C:\Users\Administrator\Desktop\out.mp4

 

Sometimes the background noise of a video taken with a mobile phone is relatively loud. How to remove the noise and replace it with a piece of wonderful music?

1
2
3
4
5
6
7
8
9
10
第一步:把源文件里的音频去掉,生成一个临时文件tmp.mov
ffmpeg -i D:\Media\IMG_0873.MOV -vcodec copy -an D:\Media\tmp.mov

注:-vcodeccopy的意思是对源视频不解码,直接拷贝到目标文件;-an的意思是将源文件里的音频丢弃。

第二步:把这个无声的视频文件(tmp.mov)与一个音乐文件(music.mp3)合成,最终生成output.mov

ffmpeg -i D:\Media\tmp.mov -ss 30 -t 52 -i D:\Media\music.mp3 -vcodec copy D:\Media\output5.avi

为了保证良好的合成效果,音乐时长必须匹配视频时长。这里我们事先知道视频时长为52秒,于是截取music.mp3文件的第30秒往后的52秒与视频合成。另外,为了保证音频时长截取的准确性,我们这里没有使用-acodec copy,而是让音频重新转码。

Video picture-in-picture function (similar to WeChat video)

1
ffmpeg -i out1.mp4  -i out2.mp4  -filter_complex "[1:v]scale=w=176:h=144:force_original_aspect_ratio=decrease[ckout];[0:v][ckout]overlay=x=W-w-10:y=0[out]" -map "[out]" -movflags faststart  out.mp4

ffmpeg -rtsp_transport tcp -i rtsp://192.168.2.174:554/ch01 -rtsp_transport tcp -i rtsp://192.168.4.21:554/ch01 -filter_complex "[1:v]scale=w=176:h=144:force_original_aspect_ratio=decrease[ckout];[0:v][ckout]overlay=x=W-w-10:y=0[out]" -map "[out]" -movflags faststart out_6.mp4

4 videos are played at the same time, upper left corner, upper right corner, lower left corner, lower right corner

1
ffmpeg -i out1.mp4 -i out2.mp4 -i out3.mp4 -i out4.mp4 -filter_complex "[0:v]pad=iw*2:ih*2[a];[a][1:v]overlay=w[b];[b][2:v]overlay=0:h[c];[c][3:v]overlay=w:h" out.mp4

 

4 videos are played at the same time, the upper left corner, the upper right corner, the lower left corner and the lower right corner, are cut according to the time

1
ffmpeg -ss 00:00:00 -t 00:00:05  -i out1.mp4 -ss 00:00:00 -t 00:00:05  -i out2.mp4 -ss 00:00:00 -t 00:00:05  -i out3.mp4 -ss 00:00:00 -t 00:00:05 -i out4.mp4 -filter_complex "[0:v]pad=iw*2:ih*2[a];[a][1:v]overlay=w[b];[b][2:v]overlay=0:h[c];[c][3:v]overlay=w:h" out.mp4

Synthetic gif

1
2
3
4
5
把视频的前 30 帧转换成一个 Gif
ffmpeg -i input_video.mp4 -vframes 30 -y -f gif output.gif

将视频转成 gif
ffmpeg -ss 00:00:00.000 -i input.mp4 -pix_fmt rgb24 -r 10 -s 320x240 -t 00:00:10.000 output.gif

 

Add watermark

1
ffmpeg -i input.mp4 -i picture.png -filter_complex overlay="(main_w/2)-(overlay_w/2):(main_h/2)-(overlay_h)/2" output.mp4

 

Three-in-one left center right synthesis

1
ffmpeg -i 1.mp4 -i 2.mp4 -i 3.mp4 -filter_complex "[0:v]pad=iw*3:ih*1[a];[a][1:v]overlay=w[b];[b][2:v]overlay=w*2" out.mp4

Three-in-one playback in turn, the resolution is 1280*720

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
ffmpeg -i 1.mp4 -s 426:720  1.mp4  //调整视频大小 426*720

截取视频第一帧
ffmpeg -i 1.mp4 -r 1 -vframes 1 -f image2 1.jpg

//视频图片合并
ffmpeg -i 1.mp4 -i 2.jpg -i 3.jpg -filter_complex "[0:v]pad=iw*3:ih*1[a];[a][1:v]overlay=w[b];[b][2:v]overlay=w*2" 4.mp4

ffmpeg -i 1.jpg -i 2.mp4 -i 3.jpg -filter_complex "[0:v]pad=iw*3:ih*1[a];[a][1:v]overlay=w[b];[b][2:v]overlay=w*2" 5.mp4

ffmpeg -i 1.jpg -i 2.jpg -i 3.mp4 -filter_complex "[0:v]pad=iw*3:ih*1[a];[a][1:v]overlay=w[b];[b][2:v]overlay=w*2" 6.mp4


//先转换格式
ffmpeg -i 4.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts input1.ts
ffmpeg -i 5.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts input2.ts
ffmpeg -i 6.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts input3.ts

//最后进行链接播放
ffmpeg -i "concat:input1.ts|input2.ts|input3.ts" -c copy -bsf:a aac_adtstoasc -movflags +faststart output.mp4

 

Adjust video rate

1
2
3
4
原理:调整视频速率的原理为:修改视频的pts,dts 此过程由于不用进行解码编码,所以费时很少
ffmpeg -i input.mkv -an -filter:v "setpts=0.5*PTS" output.mkv
注意:调整速度倍率范围[0.25, 4]  如果只调整视频的话最好把音频禁掉  对视频进行加速时,如果不想丢帧,可以用-r 参数指定输出视频FPS
ffmpeg -i input.mkv -an -r 60 -filter:v "setpts=2.0*PTS" output.mkv

 

Adjust audio rate

1
2
3
4
原理:简单的方法是调整音频采样率,但是这种方法会改变音色, 一般采用通过对原音进行冲采样,差值等方法。
ffmpeg -i input.mkv -filter:a "atempo=2.0" -vn output.mkv
注意:倍率调整范围为[0.5, 2.0] 如果需要调整4倍可采用以下方法:
ffmpeg -i input.mkv -filter:a "atempo=2.0,atempo=2.0" -vn output.mkv

 

Adjust the speed of audio and video at the same time

1
ffmpeg -i input.mkv -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]" output.mkv

 

Download video files via URL

1
ffmpeg -i http://XXX.COM/XXX.m3u8   D:/a.mp4

 

Turn pictures into long playable videos

1
ffmpeg -r 25 -loop 1 -i C:\Users\a\Desktop\0.png  -pix_fmt yuv420p -vcodec libx264 -b:v 600k -r:v 25 -preset medium -crf 30 -s 720x576 -vframes 250 -r 25 -t 10 C:\Users\a\Desktop\a.mp4

 

Convert m3u8 to mp4

1
ffmpeg -i “http://xxxxxx/video/movie.m3u8” -vcodec copy -acodec copy -absf aac_adtstoasc output.mp4

Camera h264:

ffmpeg -f video4linux2 -i /dev/video0 -s 640x480 -r 24 -vcodec libx264  -f flv rtmp://127.0.0.1/rtmpsvr/rtmp1

 

Guess you like

Origin blog.csdn.net/hyl999/article/details/114015749