Simple commands to learn FFMPEG in real use case 🎥
Understanding the most useful parameters of FFMPEG with real use-cases. I have used ffmpeg for more than 10 years with weet. With this story you can learn the most useful and simple commands on FFMPEG. These commands are much faster than other video softwares like iMovie or Final Cut Pro or Adobe Premiere 🤫.
1 — Converting a simple video
ffmpeg -i video.mov out.mp4
This is the most simple command to generate a video with default parameter:
- -i input file : this is the input file to proceed
- out.mp4 : this is the file generated by ffmpeg
You can use this command to “🛠 fix a video” that can be difficult to read on a media player, or just to optimize the size of the video.
2 — Processing video without reencoding
ffmpeg -i video.mp4 -c:v copy -c:a copy out.mp4
One of the most important parameters 😃, It allows to proceed a file without transcoding the input file (available only if your parameters don’t need to re-encode video or audio tracks) :
- -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)
ffmpeg -i video_input.mp4 -movflags faststart out.mp4
On this command we introduce the parameter -movflags faststart. This parameter moves the metadata (duration, author etc… ) of the video at the beginning instead ofthe end.
That allows a browser to read the video without downloading the entire video file. ⏱
*To accelerate this command you can add “-c:v copy -c:a copy”
4 — Creating a thumbnail from video (extract one frame)
ffmpeg -i video.mp4 -vframes 1 -ss 10.0 thumbnail.jpg
This command allows you to extract one frame 🖼 of the video to generate a thumbnail :
- -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.
I recommend 24 fps to 30 fps for a face camera video or movie 🎬. And 10 fps to 15 fps for a screencast or Powerpoint (gSlide) presentation🧑💻.
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
For the same reason as the frame rate, we don’t always needa 4K video… 🎥This resolution is really heavy, especially if you have to send to someone 📧. So you can downscale the video to have a better ratio quality / size. This command can be merged with the framerate change (chapter 5). (look at the use-case below “size of a video”). I recommend the 720p resolution for the best ratio quality, but for a screencast you have to keep 1080p to keep the font or detail intact. (I record every screencast with weet in 1080p, and it’s free)
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
This command removes the video before 10 seconds
- -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
This command removes the the video after 10.0
- -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
If you want to make a dynamic thumbnail of your video or simply create a “meme” 😃, you can create gif of your video. (Careful! The size of your gif can be really big)
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)
You can map everything with the “map” command. 🎬 Multiple audio tracks of multiple video tracks (weird but you can 🤨)
11 — Looping a video 🔁
When you want to create a long video, it can be useful to loop the video. There are a lot of different ways to loop a video with FFMPEG. This command is not the most optimized, but it works with every videos. I will not explain every parameter of this command (too complex), just replace the video.mp4 by your input filename. You can create a 10h Nyan cat video… 😝
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
If you want to generate video from an image (.png, .jpg .gif). It’s useful if you have to input an image in a software that allows only video (like YouTube ). (See section 10 to add audio to your content)
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
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 📚.