FFmpeg¶
FFmpeg is a free and open-source software library for playing and manipulating video and audio files and streams. FFmpeg is primarily a command-line tool which is used for basic editing of media files and transcoding between different file formats. The tool is available for Windows, Linux, and Apple Mac.
Personal Experience¶
My main use of FFmpeg is to quickly do simple edits to videos in batches without needing to open a fully fledged video editor. The tool can be used to quickly combine a sequence of images into a video, obtain information about an audio or video file, convert between file formats, change the resolution of a video, crop or rotate, trim to a specific duration, split it into multiple parts, merge multiple parts, remove audio from the file or save as audio only.
Tools & Software Integrations¶
FFmpeg includes the following components: - ffmpeg - A command-line tool that converts audio or video formats. The software can also capture and encode audio and video in real-time from different hardware and software sources. - ffplay - A basic command-line media player for convenient playback. - ffprobe A command-line tool to display media file information.
Recommended Plugins¶
N/A
Resources¶
FFmpeg Website - Includes detailed documentation, installation instructions and download links to pre-compiled versions of the software for Windows, Linux and Mac.
Notes and Troubleshooting¶
Install FFmpeg on Windows¶
- Open https://ffmpeg.org/download.html in a web browser.
- Click the Windows logo.
- Click Windows builds from gyan.dev which offers FFmpeg builds specifically for Windows.
- Scroll down the page to the 'release builds' section.
- Select either the downloadable file called ffmpeg-release-essentials.zip or the file ffmpeg-release-full.7z (the latter will need to be decompressed with a utility like 7zip).
- Extract the downloaded file.
- Rename the extracted folder to ffmpeg.
- Cut and paste the new folder into the root of your hard drive e.g.
C:\ffmpeg. - Open the system environment variables control panel by pressing the Windows key, typing 'system variables' into the search bar, and selecting 'Edit the system environment variables' in the search results.
- Click the Environment variables button in the bottom-right area of the window.
- Select the 'Path variable' under 'User variables for...' and click Edit.
- Click the 'New' button to open a new blank line below the bottom-most path.
- Type the path to the bin folder within the new FFmpeg folder e.g.
C:\ffmpeg\bin. - Click 'OK' and you'll see the FFmpeg path at the end of the 'Path' variable in the window.
- Click 'OK' to save your changes. You've now installed FFmpeg and set the proper environment variables.
- To confirm that FFmpeg is working, open the command prompt and run the command
ffmpeg -versionto see the version number.
Change Video Resolution¶
ffmpeg -i input.mp4 -s 1280x720 -c:a copy output.mp4
- The
-sflag sets the scale and the-c:aflag copies the audio to the newly copied ouptut file.
Convert Between Video File Fomats¶
- Convert the video by specifiying the filename and extension to convert from and to:
ffmpeg -i video.mp4 -qscale 0 video.avi
- The
-qscale 0flag preserves the quality of the source video during conversion.
Convert Video to Audio¶
- Specifiy the required input and output filenames and extensions:
ffmpeg -i input.mp4 -vn output.mp3
- The
-vnflag indicates that video recording has been disabled. - Further flags are available to set the audio frequency
-ar, the number of audio channels-ac, and the bitrate-abof the output file if required.
Crop a Video¶
ffmpeg -i input.mp4 -filter:v "crop=w:h:x:y" output.mp4
- The
-filter:vflag indicates the use of the video filter. - The
wandhvalues indicate the width and height of the rectangle to crop from the source video. - The
xandyvalues indicate the coordinate position of the rectangle area to be cropped. - NOTE: Cropping videos will affect the quality.
Display Audio/Video File Information¶
- Specify both the filename and extension to read the file metadata:
ffmpeg -i video.avi -hide_banner
- The
-hide_bannerflag removes information about the ffmpeg software version so the user can focus on the file metadata.
Display Supported File Formats¶
ffmpeg -formats
Extract Images From A Video¶
ffmpeg -i input.mp4 -r 1 -f image2 image-%3d.png
- The
-rflag sets the number of frames to be extracted per second. The default value is25. - The
-fflag indicates the output format i.e image. - The string
image-%2d.pngindicates how to name the extracted images e.g.image-01.png,image-02.png,image-03.pngetc. Here%3dspecifies the number of digits to be used in the numbering system. Using%3dresults in names starting fromimage-001.png.
Convert a Static Image Sequence to Video¶
- Try the following command:
ffmpeg -r 30 -i input-%03d.png -c:v libx264 -s 1920x1080 -r 30 -pix_fmt yuv420p ouput.mp4
- The
-r 30flags and values indicate an input and output frame rate of 30 frames per second. - The
-i %03d.pngflag indicates the format oif the input file names are e.g. sequentially-named png files with numerical names padded with zeros. - The
-c:v libx264part tells ffmpeg to use the libx264 encoder. - The
-s 1920x1080section sets the video resolution to 1920x1080. - The
-pix_fmt yuv420psection specifies the pixel format.
Fade Video In and Out¶
- The following code will apply a one second fade in from black and then end the clip with a one second fade out to black:
ffmpeg -i video.mp4 -vf "fade=t=in:st=0:d=1,fade=t=out:st=26:d=1" -c:a copy out.mp4
NOTE: The start time of each fade needs to be specified along with the duration of the fade.
Fade Audio In and Out¶
- The following code will apply a one second fade in to the start of the clips audio and then end the clip's audio with a one second fade out:
ffmpeg -i music.mp3 -af "afade=t=out:st=1:d=5,afade=t=out:st=26:d=1" out.mp3
Merge Video Files¶
- You can use the following one-liner command to join all files in a directory:
ffmpeg -i "concat:video1.mp4|vidio2.mp4|vidio3.mp4" -c copy output.mp4
- Alternatively, create a
join.txtfile that contains the exact paths of the files to merge. All files should be same format.
file /home/sk/myvideos/part1.mp4
file /home/sk/myvideos/part2.mp4
file /home/sk/myvideos/part3.mp4
file /home/sk/myvideos/part4.mp4
- Join the files using the following command:
ffmpeg -f concat -i join.txt -c copy output.mp4
- If you get an error like
[concat @ 0x555fed174cc0] Unsafe file name '/path/to/mp4' join.txt: Operation not permittedyou try adding the-safe 0flag:
ffmpeg -f concat -safe 0 -i join.txt -c copy output.mp4
Play Video Or Audio Files¶
- Play file from the comman-line terminal:
ffplay video.mp4
Record M3U8 Streaming Video¶
- Obtain the M3U8 playlist URL from the source website.
- Download and convert the media segments:
ffmpeg -i "https://example.com/playlist.m3u8" -c copy output.mp4
- FFmpeg will read the M3U8 playlist, download each media segment, and concatenate them into a single output file.
Remove Audio from Video¶
- Make a copy of the file using the
-anflag:
ffmpeg -i input.mp4 -an output.mp4
- The
-anflag mutes the audio while copying the file.
Set the Video Aspect Ratio¶
ffmpeg -i input.mp4 -aspect 16:9 output.mp4
Split Files Into Multiple Parts by Duration¶
ffmpeg -i input.mp3 -f segment -segment_time 600 -c copy output_%03d.mp3
- This would split the file into 600 second (10 minute) chunks.
Split Files Into Multiple Parts by Timestamp¶
ffmpeg -i input.mp4 -t 00:00:30 -c copy part1.mp4 -ss 00:00:30 -codec copy part2.mp4
- The first timestamp
-t 00:00:30indicates creates a clip from the start of the video to the 30th second of video. - The second timestamp
-ss 00:00:30indicates the start of a second second clip at 30 seconds in that will continue up to the end of the original file.
Trim a Media File¶
- Files can be trimmed by start time and intended duration:
ffmpeg -i input.mp4 -ss 00:00:50 -codec copy -t 50 output.mp4
- The
-ssflag specifies the starting time to trim from. -
The
-tindicates the total duration to trim to. -
Alternatively they can be trimmed by specifying a starting and ending time:
ffmpeg -i audio.mp3 -ss 00:01:54 -to 00:06:53 -c copy output.mp3
- Here
-toindicates the end time.