Skip to content

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.

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

  1. Open https://ffmpeg.org/download.html in a web browser.
  2. Click the Windows logo.
  3. Click Windows builds from gyan.dev which offers FFmpeg builds specifically for Windows.
  4. Scroll down the page to the 'release builds' section.
  5. 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).
  6. Extract the downloaded file.
  7. Rename the extracted folder to ffmpeg.
  8. Cut and paste the new folder into the root of your hard drive e.g. C:\ffmpeg.
  9. 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.
  10. Click the Environment variables button in the bottom-right area of the window.
  11. Select the 'Path variable' under 'User variables for...' and click Edit.
  12. Click the 'New' button to open a new blank line below the bottom-most path.
  13. Type the path to the bin folder within the new FFmpeg folder e.g. C:\ffmpeg\bin.
  14. Click 'OK' and you'll see the FFmpeg path at the end of the 'Path' variable in the window.
  15. Click 'OK' to save your changes. You've now installed FFmpeg and set the proper environment variables.
  16. To confirm that FFmpeg is working, open the command prompt and run the command ffmpeg -version to see the version number.

Change Video Resolution

ffmpeg -i input.mp4 -s 1280x720 -c:a copy output.mp4
  • The -s flag sets the scale and the -c:a flag 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 0 flag 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 -vn flag 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 -ab of the output file if required.

Crop a Video

ffmpeg -i input.mp4 -filter:v "crop=w:h:x:y" output.mp4
  • The -filter:v flag indicates the use of the video filter.
  • The w and h values indicate the width and height of the rectangle to crop from the source video.
  • The x and y values 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_banner flag 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 -r flag sets the number of frames to be extracted per second. The default value is 25.
  • The -f flag indicates the output format i.e image.
  • The string image-%2d.png indicates how to name the extracted images e.g. image-01.pngimage-02.pngimage-03.png etc. Here %3d specifies the number of digits to be used in the numbering system. Using %3d results in names starting from image-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 30 flags and values indicate an input and output frame rate of 30 frames per second.
  • The -i %03d.png flag indicates the format oif the input file names are e.g. sequentially-named png files with numerical names padded with zeros.
  • The -c:v libx264 part tells ffmpeg to use the libx264 encoder.
  • The -s 1920x1080 section sets the video resolution to 1920x1080.
  • The -pix_fmt yuv420p section 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.txt file 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 permitted you try adding the -safe 0 flag:
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 -an flag:
ffmpeg -i input.mp4 -an output.mp4
  • The -an flag 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:30 indicates creates a clip from the start of the video to the 30th second of video.
  • The second timestamp -ss 00:00:30 indicates 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 -ss flag specifies the starting time to trim from.
  • The -t indicates 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 -to indicates the end time.