Difference between revisions of "FFmpeg"

From TSG Doc
Jump to navigation Jump to search
 
(12 intermediate revisions by the same user not shown)
Line 33: Line 33:
  
 
==Usage==
 
==Usage==
FFmpeg needs to be run from the command line, but you can create batch scripts to automate the process for multiple files. You can also set up FFmpeg for Python: https://www.bannerbear.com/blog/how-to-use-ffmpeg-in-python-with-examples/
+
FFmpeg needs to be run from the command line, but you can create batch scripts to automate the process for multiple files. There is also a plugin for Python (https://pypi.org/project/ffmpeg-python/), but it is fairly limited so we recommend using subprocess instead (see example below).
 +
 
 
The following are examples of conversions that we commonly use.
 
The following are examples of conversions that we commonly use.
  
Line 41: Line 42:
 
<code>-i</code> specifies input file
 
<code>-i</code> specifies input file
  
The last argument is always the output file. Without specifying any codec or bit rate (see examples below), FFmpeg will encode the video using some arbitrary default settings.
+
The last argument is always the output file. Without specifying any codec or bit rate (see examples below), FFmpeg will encode the video (and audio, if included) using some arbitrary default settings, depending on the input file and output format.
 +
 
 +
===Encoding===
 +
{{cmdline|ffmpeg -i input.mp4 -vcodec h264 -b:v 8M -r:v 25 -acodec aac -b:a 256k output.mp4}}
 +
 
 +
<code>-vcodec h264</code> specifies video codec. "h264" is a shorthand for FFmpeg's x264 encoder, it is (by default) interchangeable with "x264" or "libx264".
 +
 
 +
<code>-b:v 8M</code> sets the video bit rate, in bits per second. You can use "k" or "M" as shorthand for a thousand or a million respectively. See [[Video Codecs]] for help choosing a bit rate.
 +
 
 +
<code>-r:v 25</code> sets the video frame rate, in frames per second. By default, the frame rate of the original video is kept.
 +
 
 +
<code>-acodec aac</code> specifies audio codec. AAC is used by default if your output is a .mp4 file. For AVI the default is MP3.
 +
 
 +
<code>-b:a</code> sets the audio bit rate, in bits per second. Default for AAC is 128k.
  
 
===Remove audio from video file===
 
===Remove audio from video file===
Line 56: Line 70:
  
 
===Add audio to video===
 
===Add audio to video===
{{cmdline|ffmpeg -i inputvideo.mp4 -i inputaudio.wav -vcodec copy -map 0:v -map 1:a output.mp4}}
+
{{cmdline|ffmpeg -i inputvideo.mp4 -i inputaudio.wav -vcodec copy -map 0:v -map 1:a output.mp4}}
 +
 
 +
<code>-i</code> is used twice here to add a seceond input file.
 +
 
 +
<code>-map 0:v</code> tells FFmpeg to take video from the first input (count starts at 0)
 +
 
 +
<code>-map 1:a</code> tells FFmpeg to take audio from the second input (1)
  
 
===Trimming video===
 
===Trimming video===
Line 71: Line 91:
 
<code>-b:v 8M</code> specifies video bit rate, in this case 8Mbit/s, which determines video quality.
 
<code>-b:v 8M</code> specifies video bit rate, in this case 8Mbit/s, which determines video quality.
  
 +
Note: you can use <code>-c copy</code> to trim without re-encoding, but in this case FFmpeg can only cut to the nearest keyframe, so the output may not be exactly the same length as specified.
 +
 +
===Batching===
 +
The following is a batch script (text file with .bat file extension) that converts all .mp4 files in the specified folder to .avi:
 +
<syntaxhighlight lang="batch" overflow:auto;">@echo off
 +
setlocal enabledelayedexpansion
 +
 +
:: Set source video folder, as absolute path or relative to this script
 +
set src="videos"
 +
 +
:: Set destination folder
 +
set dst="videos/converted"
 +
 +
for /R %src% %%F in (*.mp4) do (
 +
    :: Put your FFmpeg command here
 +
    ffmpeg -i "%%F" -vcodec copy "%dst%\%%~nF.avi
 +
)
 +
 +
pause
 +
</syntaxhighlight >
 +
 +
===Python===
 +
<syntaxhighlight lang="python" overflow:auto;">import subprocess
 +
 +
inputFile = "input.mov"
 +
outputFile = "output.mp4"
  
 +
subprocess.run(f'ffmpeg -i "{inputFile}" -vcodec h264 -b:v 8M "{outputFile}"')
 +
</syntaxhighlight >
 +
The command within <code>subprocess.run()</code> is the same as you would type into the command line. You can use Python's [https://docs.python.org/3/tutorial/inputoutput.html#tut-f-strings string formatting] to insert variables.
  
Note: you can use <code>-c copy</code> to trim without re-encoding, but FFmpeg can only cut to the nearest keyframe, so the output may not be exactly the same length as specified.
+
==See Also==
 +
* [[Video Codecs]]

Latest revision as of 15:56, 9 April 2024

FFmpeg
Ffmpeg logo.jpg
Installed version6.1.1
Development statusActive
Operating systemMicrosoft Windows, Linux, MacOSX
LicenseGNU LGPL 2.1
Websiteffmpeg.org

FFmpeg is an open-source command-line tool to convert audio and video files.


Installation

Windows 10

  1. Download the latest ffmpeg-release-full build on https://ffmpeg.org/download.html
  2. Unpack the 7z/zip file anywhere you like
  3. Add ffmpeg to Windows' Environment Variables:
    1. Open the start menu and start typing "Environment Variables"
    2. Select "Edit the system environment variables"
    3. Click "Environment Variables"
    4. Select "Path" and click "Edit"
    5. Click "Browse" and go to the folder where you unpacked your FFmpeg files. Select the 'bin' folder inside (e.g. "C:\ffmpeg\bin")
    6. Click OK on all previously opened windows.
  4. To test, go to the command line (Windows+R -> cmd) and type ffmpeg. It should tell you the installed ffmpeg version.

Usage

FFmpeg needs to be run from the command line, but you can create batch scripts to automate the process for multiple files. There is also a plugin for Python (https://pypi.org/project/ffmpeg-python/), but it is fairly limited so we recommend using subprocess instead (see example below).

The following are examples of conversions that we commonly use.

Basic Format Conversion

ffmpeg -i input.mp4 output.avi

-i specifies input file

The last argument is always the output file. Without specifying any codec or bit rate (see examples below), FFmpeg will encode the video (and audio, if included) using some arbitrary default settings, depending on the input file and output format.

Encoding

ffmpeg -i input.mp4 -vcodec h264 -b:v 8M -r:v 25 -acodec aac -b:a 256k output.mp4

-vcodec h264 specifies video codec. "h264" is a shorthand for FFmpeg's x264 encoder, it is (by default) interchangeable with "x264" or "libx264".

-b:v 8M sets the video bit rate, in bits per second. You can use "k" or "M" as shorthand for a thousand or a million respectively. See Video Codecs for help choosing a bit rate.

-r:v 25 sets the video frame rate, in frames per second. By default, the frame rate of the original video is kept.

-acodec aac specifies audio codec. AAC is used by default if your output is a .mp4 file. For AVI the default is MP3.

-b:a sets the audio bit rate, in bits per second. Default for AAC is 128k.

Remove audio from video file

ffmpeg -i input.mp4 -vcodec copy -an output.mp4

-vcodec copy means the video won't be re-encoded, preserving the quality.

-an strips the audio

Extract single audio channel from video file

ffmpeg -i input.mp4 -af "pan=mono|c0=c0" output.wav

-af "pan=mono|c0=c0" extracts a single channel from the file. Use c0=c0 for left channel, c0=c1 for right channel.

Add audio to video

ffmpeg -i inputvideo.mp4 -i inputaudio.wav -vcodec copy -map 0:v -map 1:a output.mp4

-i is used twice here to add a seceond input file.

-map 0:v tells FFmpeg to take video from the first input (count starts at 0)

-map 1:a tells FFmpeg to take audio from the second input (1)

Trimming video

ffmpeg -i input.mp4 -ss 00:00:04 -to 00:00:06 -vcodec h264 -b:v 8M output.mp4

-ss 00:00:04 specifies the starting time, in this case 4 seconds (use format HH:MM:SS, or HH:MM:SS.MS)

-to 00:00:06 specifies end point, in this case 6 seconds (same formatting as above)

Alternatively, instead of -to, you can use -t 00:00:02 to cut exactly 2 seconds from the starting time.

-vcodec h264 specifies video codec.

-b:v 8M specifies video bit rate, in this case 8Mbit/s, which determines video quality.

Note: you can use -c copy to trim without re-encoding, but in this case FFmpeg can only cut to the nearest keyframe, so the output may not be exactly the same length as specified.

Batching

The following is a batch script (text file with .bat file extension) that converts all .mp4 files in the specified folder to .avi:

@echo off
setlocal enabledelayedexpansion

:: Set source video folder, as absolute path or relative to this script
set src="videos"

:: Set destination folder
set dst="videos/converted"

for /R %src% %%F in (*.mp4) do (
    :: Put your FFmpeg command here
    ffmpeg -i "%%F" -vcodec copy "%dst%\%%~nF.avi
)

pause

Python

import subprocess

inputFile = "input.mov"
outputFile = "output.mp4"

subprocess.run(f'ffmpeg -i "{inputFile}" -vcodec h264 -b:v 8M "{outputFile}"')

The command within subprocess.run() is the same as you would type into the command line. You can use Python's string formatting to insert variables.

See Also