Skip to end of metadata
Go to start of metadata

ffmepg is a integrated tool for video encoding/decoding processing as open source project - http://ffmpeg.org


Typical syntax of ffmpeg command is like below:

ffmpeg [global_options] {[input_file_options] -i input_url} ... {[output_file_options] output_url} ...


1. Get Video Information

$ ffmpeg -i video.mp4


2. Get Video Information without bannder

$ ffmpeg -i video.mp4 -hide_banner


3. Converting video to mp4

$ ffmpeg -i video.avi video.mp4


4. Converting video to mkv

$ ffmpeg -i video.avi video.mkv


5. Converting video to mp4 without any quality loss

$ ffmpeg -i input.webm -qscale 0 output.mp4


6. Displaying the supported formats by ffmpeg

$ ffmpeg -formats


7. Converting video files to audio files

$ ffmpeg -i input.mp4 -vn -ab 320 output.mp3


8. Change resolution of video files

$ ffmpeg -i input.mp4 -filter:v scale=1280:720 -c:a copy output.mp4

or

$ ffmpeg -i input.mp4 -s 1280x720 -c:a copy output.mp4


9. Compressing video files

$ ffmpeg -i input.mp4 -vf scale=1280:-1 -c:v libx264 -preset veryslow -crf 24 output.mp4

Note crf value 24 is too aggressive, you'd better lower its value, 


10. Compressing audio files

$ ffmpeg -i input.mp3 -ab 128 output.mp3


11. Extracting images from video

$ ffmpeg -i input.mp4 -r 1 -f image2 image-%2d.png
  • -r – Set the frame rate. I.e the number of frames to be extracted into images per second. The default value is 25.
  • -f – Indicates the output format i.e image format in our case.
  • image-%2d.png – Indicates how we want to name the extracted images. In this case, the names should start like image-01.png, image-02.png, image-03.png and so on. If you use %3d, then the name of images will start like image-001.png, image-002.png and so on.


12. Split video files into multiple parts

$ ffmpeg -i input.mp4 -t 00:00:30 -c copy part1.mp4 -ss 00:00:30 -codec copy part2.mp4


Below is an advanced version of splitting video files showing the way to implement one command instead of multiple command even though that does not reduce efforts to type the necessary options.

echo "Two commands" 
time ffmpeg -v quiet -y -i input.ts -vcodec copy -acodec copy -ss 00:00:00 -t 00:30:00 -sn test1.mkv
time ffmpeg -v quiet -y -i input.ts -vcodec copy -acodec copy -ss 00:30:00 -t 01:00:00 -sn test2.mkv
echo "One command" 
time ffmpeg -v quiet -y -i input.ts -vcodec copy -acodec copy -ss 00:00:00 -t 00:30:00 \
  -sn test3.mkv -vcodec copy -acodec copy -ss 00:30:00 -t 01:00:00 -sn test4.mkv


13. Joining multiple video parts into one

$ ffmpeg -f concat -i join.txt -c copy output.mp4

For your information, join.txt contains following video files

C:\Users\LifePlanet\Downloads\part1.mp4
C:\Users\LifePlanet\Downloads\part2.mp4
C:\Users\LifePlanet\Downloads\part3.mp4
C:\Users\LifePlanet\Downloads\part4.mp4


14. Adding subtitles to a video file

$ fmpeg -i input.mp4 -i subtitle.srt -map 0 -map 1 -c copy -c:v libx264 -crf 23 -preset veryfast output.mp4