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 43: |
Line 43: |
| | | |
| 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 using some arbitrary default settings. |
| + | |
| + | ===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. |
| + | |
| + | <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 78: |
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 FFmpeg can only cut to the nearest keyframe, so the output may not be exactly the same length as specified. | + | 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=== | | ===Batching=== |
| The following is a batch script (text file with .bat file extension) that converts all .mp4 files in the specified folder to .avi: | | The following is a batch script (text file with .bat file extension) that converts all .mp4 files in the specified folder to .avi: |
− | <pre>@echo off | + | <syntaxhighlight lang="batch" overflow:auto;">@echo off |
| setlocal enabledelayedexpansion | | setlocal enabledelayedexpansion |
| | | |
Line 97: |
Line 110: |
| | | |
| pause | | pause |
− | </pre> | + | </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 string formatting to insert variables. |
| | | |
| ==See Also== | | ==See Also== |
| * [[Video Codecs]] | | * [[Video Codecs]] |