7. ffmpeg 笔记 中
Merge All Videos in a Folder
1 | find *.mp4 | sed 's:\ :\\\ :g'| sed 's/^/file /' > fl.txt |
Video Compression
1 | // Video encoded with h.264, audio encoded with aac |
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 | ffmpeg -i input.mp4 -c:v libx264 -crf 23 output.mp4 |
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.
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 | ffmpeg -i image.jpeg -vf scale=413:626 2-inch.jpeg |
Repeat an audio file 10 times:
1 | ffmpeg -stream_loop 10 -i input.m4a -c copy output.m4a |