Simple commands to learn FFMPEG in real use case 🎥

1 — Converting a simple video

ffmpeg -i video.mov out.mp4
  • -i input file : this is the input file to proceed
  • out.mp4 : this is the file generated by ffmpeg
Simple command that optimizes video size (4,3M to 1.1M)

2 — Processing video without reencoding

ffmpeg -i video.mp4 -c:v copy -c:a copy out.mp4
  • -c:v copy : copy the video (v) track to the output file without transcoding (no processing on this track)
  • -c:a copy : copy the audio (a) track to the output file without transcoding (no processing on this track)

3 — Transcoding a video for the web (streamable)

Chrome reads the video file without downloading the entire file.
ffmpeg -i video_input.mp4  -movflags faststart out.mp4

4 — Creating a thumbnail from video (extract one frame)

ffmpeg -i video.mp4 -vframes 1 -ss 10.0 thumbnail.jpg
  • -vframes 1 : Proceeds only one frame of the video
  • -ss 10.0 : Grabs the frame at 10 seconds
  • .jpg : ffmpeg detects the output format with the extension of the file, so if you need an image, name your output file with a .jpg or .png extension.

5 — Changing the framerate of a video

A lot of recorders generate videos at 60 frames per seconds. It’s a very high quality, but sometimes we need a better ratio quality/size.

ffmpeg -i video.mp4 -r 10 -c:v copy -c:a copy out.mp4
  • -r 10 : defines number of frame per seconds to generate on the output file
  • -c:v copy: We already viewed this parameter, but this is a good case to show how this parameter is important. You don’t need to transcode new frames on your output file, just copy 10 frames per second on the output file. With this command, the output file is generated in less than a second .

6 — Scaling down (change resolution) of a video

720p vs 1080p (5.9mo and 11mo)
ffmpeg -i video.mp4 -vf scale=-2:720 out.mp4
  • -vf scale=-2:720 : asks to rescale the video with width x height (-2 =auto, 720 = height)

7 — Trim a video (Cut beginning or end of the video) 🪚

If you want to trim a video (cut the beginning or the end of the video), you just need to tell to ffmpeg where to start or end the processing of the video.

ffmpeg -i video.mp4 -ss 10.0 out.mp4
  • -ss 10.0 : starts the input at 10 seconds (so remove the 0 to 10.0s of the video)
ffmpeg -i video.mp4 -t 10.0 out.mp4
  • -t 10.0 : generates the video only for 10.0s (so removes the 10.0s to the end of the video)

8— Create a Gif thumbnail from video

Gif thumbnail generated with ffmpeg 🌵
ffmpeg -i video.mp4 -ss 10.0 -t 5.0 -vf scale=-2:240 -r 7 -loop 0 thumbnail.gif
  • -r 7 : asks to generate only 7 frames per second, (it’s a gif not a video 😃)
  • -loop 0 : the output will loop at the end 🔁
  • .gif : defines a gif format for the output file

9 — Removing audio from a video 🤐

Sometimes, it could be good to remove audio from a video (Hyper noisy environment). We simply remap the tracks on our video with only the video track.

ffmpeg -i video.mp4 -map [0:v] out.mp4
  • -map [0:v]: maps the track video (v) from the first input (0)

10 — Replacing audio with an mp3 file 🎼

With the “map” command, you can remap each track like you want. This is useful when you need to add/change an audio track

ffmpeg -i video.mp4 -i audio.mp3 -map [0:v] -map [1:a] out.mp4
  • -map [0:v]: maps the track video (v) from the first input (0)
  • -map [1:a]: maps the audio track (a) from the second input (1)

11 — Looping a video 🔁

Example of a 10 hours video (don’t click on ▶️ ! )
ffmpeg -re -f lavfi -i "movie=filename=video.mp4:loop=0, setpts=N/(FRAME_RATE*TB)" -t 600 out.mp4
  • filename=video.mp4: names of the video input
  • -t 600: duration in seconds of the video output (36,000 for 10 hours 🥴)

12 — Creating video from an image

Example of image to video use case
ffmpeg -loop 1 -i image.jpg -t 600 out.mp4
  • -loop 1: loops the input
  • -t 600: duration in seconds of the video output

13 — Launching ffmpeg in background

When you use ffmpeg on a server side or simply in your local machine, it can be really useful to launch ffmpeg in background. You can do it with the command “nice”. With that, you can launch a lot of ffmpeg commands on your computer. ffmpeg will use only the CPU not used by another application.

nice -n 19 ffmpeg -i input.mkv out.mp4
  • nice -n 19 : asks to the command to launch in priority 19 (20 is the lowest and -20 the highest) more information here

Conclusion

I hope this command will help you to learn FFMPEG. These commands are really useful and faster than other video softwares. You have to continue to edit with this software when you create video, but for a lot of simple use cases (like web conversion, cut, gif conversion) ffmpeg is so much faster ! (yes I’m a fanboy 🙋). You can find more informations in the ffmpeg documentation 📚.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jeremy Rouet

Jeremy Rouet

8 Followers

Hi, I’m Jeremy Rouet. I’m CTO / cofounder at https://weet.co and https://speach.me. I love imagining new features / concepts.