ffmpeg 笔记

Merge All Videos in a Folder
1
2
3
4
5
find *.mp4 | sed 's:\ :\\\ :g'| sed 's/^/file /' > fl.txt
ffmpeg -f concat -i fl.txt -c copy output.mp4
// Ignore error messages
ffmpeg -safe 0 -f concat -i fl.txt -c copy output.mp4
rm fl.txt

Reference

Video Compression
1
2
3
4
5
6
// Video encoded with h.264, audio encoded with aac
ffmpeg -i input.mp4 -vcodec h264 -acodec aac output.mp4
// Video encoded with h.265, compressed to a smaller file
ffmpeg -i input.mp4 -vcodec libx265 -crf 28 output.mp4
// Video encoded with h.264, preserving better quality
ffmpeg -i input.mp4 -vcodec libx264 -crf 20 output.mp4

The smaller the crf, the higher the video quality; the larger the crf, the smaller the video file.

Encoding parameters can also be abbreviated, changing from -vcodec and -acodec to -c:v and -c:a:

1
2
3
ffmpeg -i input.mp4 -c:v libx264 -crf 23 output.mp4
ffmpeg -i input.mp4 -c:v libx265 -crf 28 output.mp4
ffmpeg -i input.mp4 -c:v libvpx-vp9 -crf 31 -b:v 0 output.mkv

Reference

Both AVC/H264 and HEVC/H265 are software encoders and are very slow. You can choose NVIDIA’s hardware encoders: hevc_nvenc and h264_nvenc, which use hardware acceleration and are very fast.

Reference

Using NVIDIA graphics card for encoding:

1
ffmpeg -i video.mp4 -c:v hevc_nvenc -crf 28 output.mp4

Transcoding a video from H.264 to H.265 took 55 minutes, reducing the video size from 3.8GB to 430MB, with immediate results. Transcoding command: ffmpeg -i 1.mp4 -c:v libx265 -vtag hvc1 -c:a copy 1_hevc.mp4

On Windows 10, you can install ffmpeg using scoop and update all programs installed via scoop on Windows with:
scoop list | foreach { scoop update $_.Name }.

Trim a video with the same encoding according to a specified time:

1
ffmpeg -ss 00:05 -to 08:53.500 -i ./input.mp4 -c copy video.mp4

Quickly edit a video using ffmpeg:

1
ffmpeg -ss 07:18 -to 13:45 -i ./aaa.mkv -c copy bbb.mkv
  • -ss indicates the start time
  • -to indicates the end time
  • -i is the input file
  • -c means using the same encoding as the original video
  • bbb is the name of the output file

Merge video and audio, keeping the video encoding unchanged and changing the audio encoding to aac:

1
ffmpeg -i 1.mp4 -i 1.opus -c:v copy -c:a aac output.mp4

Convert PNG format images to JPG format:

1
ffmpeg -i image.png -preset ultrafast image.jpg

Resize an image:

1
2
ffmpeg -i image.jpeg -vf scale=413:626 2-inch.jpeg
ffmpeg -i image.jpeg -vf scale=390:567 1-inch.jpeg

Repeat an audio file 10 times:

1
ffmpeg -stream_loop 10 -i input.m4a -c copy output.m4a