Blog | FFMPEG for Beginners: Scripts for Processing, Converting, and Streaming Video
Before we dive in, this beginner’s manual will contain some general purpose information and then some information that’s specific to Mac users and Python users. Ffmpeg is an amazing set of tools, but it’s also available in a variety of flavors. If the sections for Mac and then Python don’t apply to you, you may need to supplement this beginner’s primer with other online content.
The guide is to help ease you into using ffmpeg by explaining some basics about how scripts are structured, provide resources that will help you, walk you through some popular commands, and describe some common pitfalls you might encounter.
If you work with video, you’ve heard of ffmpeg. You may even have heard of it before you know exactly what it does, but it’s used everywhere in video projects. Ffmpeg stands for Fast Forward MPEG. It’s an opensource software project for working with audio and video. It is command-line based and lets you handle video, audio, multimedia files, and streams. You’ll find it used for transcoding, simple editing, video scaling, video post-production effects and standards compliance.
If you’re curious about what MPEG stands for, it’s Moving Picture Experts Group. This group is made up of the ISO and IEC working groups, which set standards for media coding, compression coding of audio, video, graphics, and genomic data, and transmission and file formats for various applications.
What’s the FFmpeg Project?
The ffmpeg project has four major applications:
- FFmpeg — command-line interface that lets you work with your video, audio, or other media.
- FFplay — a media player.
- FFserver — a streaming media web server.
- FFprobe — a stream analysis tool.
In addition to there being four major applications, there’s tons of libraries for each application that help you accomplish tasks in different languages and environments. We can’t cover everything here, so we’ll focus on ffmpeg for now, and some of the basics you’ll want in order to start understanding this tool.
Here are some great resources for working with ffmpeg:
- FFmpeg.org — This is everything for the ffmpeg community. Check out download options, documentation, their community, report bugs, read the source code, add to ffmpeg if you want, or hire someone to help you if it’s all too much.
- FFmpeg Docs — This is linked off the website listed above, but if you want to go straight to a list of all the different options for scripts, go here.
- FFmpeg Bug Tracker and Wiki — It’s for reporting, tracking, and resolving bugs but it’s a lot more. If you spend some time digging around here, you can find helpful information about how to set up scripts for various features you may be interested in. For example, they have a Streaming Guide and a page about Encoding for Streaming Sites.
Ffmpeg CLI can be a pain to install. There’s an easy way to install with homebrew though, and we detail that in the start of the Live Stream to the Browser with FFMPEG CLI and Python .
After you have it installed, test it works by checking the version:
FFMPEG Command Structure
Commands in ffmpeg have two basic structures. (We’ll put the commands we use for examples again in the Popular FFMPEG Commands section.) One is like the command you just ran in the Getting Started section, where you’re trying to find something out about ffmpeg itself. And the other more common one has this structure:
ffmpeg input output
Here’s an example command to show you what this structure might look like in action:
ffmpeg -i video1.avi -s 320x240 -vcodec msmpeg4v2 video2.avi
Here you’re saying: Let’s use the ffmpeg CLI (ffmpeg). There’s nothing you need to know about the file’s format, but the input file (-i) is video1.avi. Next, you start describing the output. For the output you want to have a file that has a frame size (-s) of 320x240 pixels. You want to transcode using the video codec msmpeg4v2 (-vcodec msmpeg4v2) and output to a file that will be called video2.avi.
ffmpeg commands will usually follow this structure. Written generally, it looks like:
ffmpeg [format details for the input][input file or device] [format details for the output][output file or device]
The structure is important to get right — a common mistake is to put format details for the input after the -i which won’t work. What’s meant by format details is things like what file format or framework you want to use, the frame rate, screen size, and any other flags you may find that you want to try. After the -i usually all you add for input is the devices or the file you’ll be using.
Popular FFMPEG Commands
These are some useful commands you can try with your video projects. There’s lots more out there, these are just to help you get started.
Get File Info
ffmpeg -i video.flv
This command lets you get information about a video file. Note how it follows the structure we talked about — you put the file you want details about after the -i. You don’t have to worry about input/output for this command.
Play a Video from the Terminal
This is a great command because you can test your work. After you edit a file in some way, you can play the new file you create in the terminal to see if everything went okay.
Split a Video Into Images
ffmpeg -i video.flv image%d.jpg
In this command, you specify a video in the current directory and then split it into frames. The original copy of the file will be retained. Every image will be named using the pattern you specify with the part of the script
Convert Images Into a Video
ffmpeg -i image%d.jpg output.mp4
In this command you put the name of the last image from your sequence of images in place of
image%d.jpg. You can start from a lower number to create a video with less images out of the total number you have if you like. Photos need to be named uniformly to be merged, for example image3.jpg, image2.jpg, image1.jpg.
Change Container Formats Without Re-Encoding
ffmpeg -i input.mp4 -c:v copy -c:a copy output.mov
Some tools require a specific video format and re-encoding can take awhile. In certain situations, you can save yourself time by changing the container format without doing any re-encoding. Not all containers are compatible with all video types, so you may need to try out a few things before finding something that works. This script is the basic command. You’re saying for input (-i) take the file input.mp4 and its video codec (-c:v) and copy it. Then take input.mp4’s audio codec (-c:a) and copy it. Place all these changes into a new file called output.mov. This file has all the same codecs, but it now has the container .mov.
Convert a Video Into an Animated GIF
ffmpeg -i video.flv animated.gif
Turn a video into a gif! The command for this one is pretty simple too.
Live Stream to api.video From Your Mac’s Camera
ffmpeg -f avfoundation -framerate 25 -i "0:0" -f flv rtmp://broadcast.api.video/s/streamKeyHere
We have a whole tutorial using this command here — Live Stream to the Browser with FFMPEG CLI and Python .
If you’re wondering what the “0:0” means we explain that in the tutorial!
Edit a File Without Re-Encoding
ffmpeg -ss 00:00:05 -i input.mp4 -t 00:00:10 -c:v copy -c:a copy excerpt.mp4
Use this command to make simple edits to your video without re-encoding.
In this command you are saying use the ffmpeg CLI (ffmpeg) and seek forward five seconds into the video clip ( — s 00:00:05) called input.mp4. From there, move forward 10 seconds (-t 00:00:10) and extract the video from the start to endpoint. If you don’t put the endpoint with the -t flag, then the video before the start point is cut off, and the rest of the video is included. The rest of the script looks familiar by now — it’s just like the changing the container script. You’re going to copy the video and audio codecs from the input video to your video excerpt, then output your new video into a file called excerpt.mp4. You can do light editing this way.
Crop a Video
ffmpeg -i input.mp4 -filter:v "crop=w:h:x:y" output.mp4
Crop a video to a size you want using the crop filter. The filter part of the script is
-filter:v "crop=w:h:x:y". The x and y specifies the coordinate of the rectangle that you want to crop from the source video, and then w and h represent the height and width of the rectangle you will crop. The outcome of the crop lowers the quality of your video, so use this sparingly or when you don't need amazing quality at the end.
Change Playback Speed
ffmpeg -i video.mpg -vf "setpts=0.5*PTS" highspeed.mpg
PTS stands for presentation timestamp. This is the time at which the frames in your video should be presented by default. When you multiply by a fraction, you’re basically dividing the timestamp, so it’s going to run through the video more slowly. If you multiply by a number, it’s going to go faster. In this use of the script, the output file will run at half the speed of the original.
Convert Video to Audio
ffmpeg -i input.mp4 -vn output.mp3
If you just wanted the audio from a video file, you can use this command to create an audio only file.
Change the Volume on an Audio File
fffmpeg -i input.mp3 -af 'volume=0.5' output.mp3
Take an audio file, then cut the volume in half and output it to a new file. Similar to the playback script, you’re multiplying the original volume by a value to get the new value for the volume.
Change Resolution for Video File
ffmpeg -i input.mp4 -filter:v scale=1280:720 -c:a copy output.mp4
You can change the video resolution for a video file with this script. Use a filter on the video with -filter:v and then choose your resolution, in this case scale=1280:720. Then you copy the audio codec because you aren’t changing it, and output your changes to the new file output.mp4.
These are just a few interesting commands you can do with ffmpeg! We hope they give you a sense of what’s possible with the tool, and make you feel more confident about building and tweaking your own scripts. Have fun!
Originally published at https://api.video.