Top10 Commands for Lossless Video Manipulation using ffmpeg (Guide)
ffmpeg is an amazing cross-platform open-source software with many different uses (you can use it for audio encoding for instance), but this guide will focus exclusively on the top ffmpeg commands for lossless editing. By this, we mean manipulating compressed formats without the need for re-encoding, not manipulating lossless formats such as png for images, flac for audio, or x265 --lossless for video, though this can also be done with ffmpeg. Because ffmpeg can handle both encoding and lossless operations, existing guides can be confusing as to what can be achieved with ffmpeg losslessly.
Advantages of Lossless Editing vs Re-Encoding:
- Extremely Fast (typically almost instantaneous)
- No Quality Loss (especially important for compressed video)
- Easy (simple command line interface with no complex parameters to specify)
Tips for Windows users:
- If you don't already have a copy, download and install ffmpeg
If you not used to command line programs the following detailed install instructions with screenshots may be useful: https://windowsloop.com/install-ffmpeg-windows-10/
For advanced use case, the actual build may matter ! (latest x264/x265 release, 10bit x265 video support, libfdk_aac encoder)
- adding ffmpeg to the Path (Recommended)
Add the directory where ffmpeg.exe is located to the Path Environment Variable (In the advanced system properties, Win10: search for env)
- Open a command prompt (cmd.exe) in the directory where your files are located and enter the proposed commands.
- OR use a batch script (.bat, Recommended): avoids having to enter command line commands and can be used to batch-process multiple video files. Be aware that the syntax changes slightly in a .bat file: each "percent" sign (%) must be doubled (%%)
- OR maybe use a GUI front-end, if you can find one that implements these commands.
+ Reply to Thread
Results 1 to 11 of 11
Last edited by butterw; 20th Mar 2021 at 17:07. Reason: Guide completed, Added windows Install instructions
Our selection of simple commands for lossless manipulation of audio/video files using ffmpeg:
ffmpeg.exe is a command-line tool. It needs to be added to your Path for the following commands to work (or you could just copy or move it to the current working directory) .
In the following examples (to be entered at the command prompt), input and output audio/video files should be in the current working directory.
To test your installation and get the current version/build of the tool: ffmpeg -version
To avoid displaying header information each time you run a ffmpeg command: -hide_banner
ffmpeg commands are based on a transformation from an input container to an output container (The format, ex .mp4, is normally auto detected for input files and guessed from the file extension for output files). This guide is centered mainly around mp4/mkv files, but ffmpeg supports many different input/output containers and can be used to convert between them as required.
- multiple inputs and ouputs are possible.
- paths\filenames need to be in quotes if they contain spaces.
- the input (-i) and output containers typically contain multiple streams (v for video, a for audio, s for subtitles)
- output options and less frequently input options (set before -i) can be specified.
10. Display information on the input file
The command can be used to get the stream layout of the input file (useful for the map command).
ffmpeg -i "input.mp4"
9. Extract audio track
We discard the video stream (-vn) at the input (input options are specified before -i) and copy the audio stream (-c:a copy) to the output.
The output audio extension needs to match the audio in the input file, otherwise the audio will get re-encoded (.mp3 file uses a different audio codec than .aac) !
ffmpeg -vn -i "input.mp4" -c:a copy "output_audio.m4a"
8. Mute a video (extract the video stream)
We copy the input video stream (-c:v copy) and discard the audio stream (-an).
ffmpeg -hide_banner -i "input.mp4"-c:v copy -an "output_muted.mp4"
7. Convert/remux the container (ex: .mp4 to .mkv, mov to .mp4 or .aac to .m4a)
Remux is done implicitly with ffmpeg commands. Remuxing may help correct some file problems (ex: missing or incorrect duration in aac audio). It discards non-essential metadata by default (ex: Encoded date, written by handbrake).
We copy all streams from the input to the output (-c copy).
ffmpeg -i "input.mp4" -c copy "output.mp4"
ffmpeg -i "input.mp4" -c copy "output.mkv"
ffmpeg -i "input.mov" -c copy "output.mp4"
ffmpeg -i "input.aac" -c copy "output.m4a"
6. Mux video and audio
In this case we have multiple input files. The -map command is used to select the output streams.
We take the video stream from input 0 and the audio stream from input 1.
ffmpeg -i "video.mp4" -i "audio.m4a" -c copy -map 0:v -map 1:a "output.mp4"
Please note that the output video file will be extended to the length of the audio stream if it is longer than the video (this could be used to continue playing audio and display subtitles after a short video intro)
If this is undesirable, the -shortest option will cause the output duration to match the duration of the shortest input stream:
ffmpeg -i "video.mp4" -i "audio.m4a" -c copy -map 0:v -map 1:a -shortest "output.mp4"
To crop the audio to the duration of the video, you can open and save the file in Avidemux.
5. Mux video and subtitles (.mkv)
Last edited by butterw; 26th Apr 2021 at 14:54.
4. Extract a segment from a video (cut on keyframes)
ffmpeg -ss 00:01:13 -i "input.mp4" -t 45 -c copy -avoid_negative_ts make_zero "output_cut.mp4"
Start time: -ss from the start OR -sseof from the end with a negative value
Time format: 00:01:13.000 or 73 (in seconds)
Duration -t OR Stop -to (you can't use both -ss and -to)
To append clips (with the same encoding parameters) -avoid_negative_ts make_zero is required to correct the timestamps of the parts.
ffmpeg -safe 0 -f concat -i concat.txt -c copy "output_merge.mp4"
concat.txt contains the relative paths to the files to join:
file 'out/tmp1.mp4' file 'out/inter.mp4' file 'out/tmp2.mp4'
3. Split a video into multiple parts based on duration (ex: 20min segments, cut on keyframes)
ffmpeg -i "input.mp4" -c copy -segment_time 00:20:00 -f segment "output%03d.mp4"
Splits occur on the nearest keyframe following the requested time, so each new segment always starts with a keyframe ensuring it is playable.
To split based on filesize or on chapters: use mkvtoolnix/mkvmerge instead.
The following advanced features are only applicable for a specific container or video codecs. They must also be supported by the video player.
2. Set DAR to correct the Display Aspect Ratio (container DAR supported in .mp4, .mkv, etc.)
ffmpeg -i "input.mp4" -aspect 4:3 -c copy "output_DAR.mp4"
1.a Rotate (Flip) a mp4 video 90° (.mp4 metadata)
Clockwise 90° Video Rotation in .mp4 (Flip Right):
ffmpeg -i "input.mp4" -c copy -metadata:s:v:0 rotate=90 "output_rot90.mp4"
ffmpeg -i "input.mp4" -c copy -metadata:s:v:0 rotate=270 "output_rot270.mp4"
1.b Optimize mp4 for the web
By moving the moov atom to the start of the file, mp4 videos can be played while they are downloading (enables progressive download, recommended for video clips uploaded to Videohelp).
ffmpeg -i "input.mp4" -c copy -movflags faststart "output_web.mp4"
A mp4 file without a moov atom (for instance a truncated non faststart file) will not play and cannot be remuxed (error: moov atom not found !). Repairing the file requires a reference file or at least the framerate of the video !
0. Video bitstream modification (h264, hevc)
More info about ffmpeg video bitstream filters here: https://ffmpeg.org/ffmpeg-bitstream-filters.html#h264_005fmetadata
Color range limited/full flag, etc.
Cropping without Re-encoding:
Set the frame cropping offsets in the SPS. This will crop 128 pixels at the bottom of a x264 video. Tested with mpc-hc, Vlc and MxPlayer on Android.
ffmpeg -i "input.mp4" -codec copy -bsf:v h264_metadata=crop_bottom=128 "output_cropped.mp4"
To specify multiple values:Code:
! It is necessary to specify the display Aspect Ratio for correct display when crop parameters are used:
ffmpeg -i "input_720p.mp4" -codec copy -bsf:v h264_metadata=crop_bottom=128 -aspect 1280:592 "output_cropped.mp4"
Last edited by butterw; 18th May 2021 at 05:24. Reason: +avidemux scripts
Convert fake stereo to mono. !!! This isn't actually a lossless operation
EDIT: The issue is that using ffmpeg -af forces an audio re-encode. Likely because compressed audio channels are not encoded independently with modern audio codecs.
While re-encoding audio should be avoided if possible, it is far less critical than for video (re-encoding is fast, quality degradation isn't obvious, transparency is achieved at far lower bitrates).
Using a lossless audio codec (ex: flac) during editing is also certainly practical (re-encode only once).
Video audio encoded at higher bitrates is becoming more common. Depending on application, re-encoding fake stereo to mono could make sense to save bitrate (ex: with aac 128kbps fake stereo can be re-encoded to 64kbps mono). 128kbps is about 1MB/minute.
ffmpeg -hide_banner -i "input.m4a" -vn -af "pan=mono|FC=FR" "output_mono.m4a"
the default ffmpeg aac bitrate is 70kbps for mono.
It is recommended to use the fraunhaufer libfdk_aac encoder (but many ffmpeg builds don't include it !!!) over the default aac encoder:
ffmpeg -hide_banner -i "input.m4a" -vn -af "pan=mono|FC=FR" -c:a libfdk_aac -b:a 64k "output_mono.m4a"
Avidemux includes the libfdk_aac encoder.
What is fake stereo (Dual Mono) ?
Fake stereo is when the audio stream has left and right channels, but these 2 channels are in fact identical.
The main benefit to converting fake stereo to mono is to reduce the bitrate by half. Fake stereo is typically encoded at the same bitrate as real stereo (ex: 128kbps aac), which is likely overkill for non music content. The transformation is done by discarding one of the two channels (in the following example we are keeping the Front Right, FR, channel).
In many cases, home recordings have a very weak stereo effect because of the short physical distance between the two microphone inputs.
You can check the correlation measure between the 2 audio channels with Audacity:
Audacity > Effect > Vocal Reduction and Isolation... > Action: Analyze > The Left and Right channels are correlated by about 100%
For near 100% correlation, there may be no point in keeping both channels even if it isn't strictly fake stereo.
Last edited by butterw; 8th Apr 2021 at 08:27.
5. Mux video and subtitles (.mkv)
Subtitles are typically embedded in mkv files, but are usually kept as separate files with mp4.
ffmpeg -i "video.mp4" -i "subtitle.srt" -c copy "output.mkv"
Supported subtitle formats by ffmpeg for .mkv: srt, ass, ssa and PGS (Bluray image based subtitles)
ffmpeg can also extract/convert text-based subtitles: https://trac.ffmpeg.org/wiki/ExtractSubtitles
In mp4, only mov_text embedded subtitles are supported
ffmpeg -i "video.mp4" -i "subtitle.srt" -c:v copy -c:a copy -c:s mov_text -metadata:s:s:0 language=eng "output.mp4"
If there is more than one video, audio or subtitle stream the map command is required to select the desired output.
ffmpeg -i video.mp4 -i subtitle1.srt -i subtitle2.srt -map 0 -map 1 -map 2 \ -c copy -metadata:s:s:0 language=eng -metadata:s:s:1 language=idk output.mkv
Last edited by butterw; 23rd Aug 2020 at 13:20. Reason: added supported subtitle formats by ffmpeg for .mkv
Any trick to getting VobSubs to mux? No luck with those ...
Is there a GUI for lossless FFmpeg operations ?
Users may justifiably struggle to get the command-line to do what they want beyond the simple use cases at first (and a GUI is likely more practical to visualize complex stream mapping cases for instance). FFmpeg, however, does not have an official, updated GUI front-end.
Mkvtoolnix, the official GUI of the command line tool mkvmerge, with the limitation that it can only output mkv (but as we've seen previously, ffmpeg can be used to convert mkv to another format... However be aware that different container formats don't all support the same features).
Cutting/Appending video clips is easier done in a lossless video cutter GUI, such as Avidemux.
Finally, many simple commands can be launched with a .bat file, as will be covered in the following post.
Last edited by butterw; 11th Mar 2021 at 10:19.
Batch commands and simple scripts
batch-processing multiple video files in the current working directory (command line version):
Converts all filename.mov files in the current directory to filename_out.mp4. The input .mov files will not be deleted.
for %f in (*.mov) do ffmpeg -hide_banner -i %f -c copy "%~nf_out.mp4"
Using a script avoids having to enter commands at the command line prompt.
Example windows batch file, copy/paste in a text-editor such as Notepad++ and save as mov2mp4.bat:
Be aware that the syntax changes slightly in a .bat file: each "percent" sign (%) must be doubled (%%).
a .bat file can be launched by double-clicking (in this case output files will be created in the .bat directory) or by command-line.
echo off REM basic ffmpeg Windows batch file. Converts/Saves all .mov files in the current directory as filename_out.mp4 for %%f in (*.mov) do ffmpeg -hide_banner -i %%f -c copy "%%~nf_out.mp4" pause
A windows .bat with the commands listed in this guide is available here:
To use it comment/uncomment the comment lines (REM) with the desired command in a text editor and save before launching the .bat.
By default, you will be prompted for the input filename and processing will begin.
Last edited by butterw; 25th Aug 2020 at 12:05. Reason: ffm4b.bat Uploaded !
use a GUI front-end, if you can find one that implements these commands
-> GoTo "RunFFmpeg".
Unlike other GUIs, that actually compose CommandLines, and then pass them to 'FFmpeg',
'RunFFmpeg' takes the CommandLines themselves to do the Job via the FFmpeg-CommandLine-Tools
'ffmpeg.exe', 'ffprobe.exe' and 'ffplay.exe' (static or shared).
Advantages, compared to Batchfiles, Scripts or other GUIs:
Life could be so easy, if there weren't those f*** Details.
- More comfortable than BatchCommands or Scripts
- ONE App for ALL Multimedia-Tasks, like
Streaming/Playing, Converting, Ripping, USB-Recording, ScreenRecording with Audio, and so on
- Total Control over the Programflow realizable by Reading from/Writing to the FFmpeg-Process
- Open-source GUI-BasicStructure, thus totally adaptable to any Task and for anyone's Needs.
Hey there, when using metadata crop on a video with buffer dimensions you have to crop this buffer pixels too.
For example a video 1920x1080 with buffer dimensions 1920x1088, if you crop 2 pixels from the bottom it will not do anything. But i dont understand how it works, if i want to crop 2 pixels from the top and 2 from the bottom. Which values do i use in this case, i tried several combinations without success, the bottom gets cropped, but not the top.
Cropping in x264 and getting it to play back correctly can be troublesome.
With a 720p input, the output is 1280x712 (viewed in Avidemux) with the following command:
ffmpeg -i "input.mp4" -codec copy -bsf:v h264_metadata=crop_top=4:crop_left=0:crop_right=0: crop_bottom=4 "output_cropped.mp4"
The alternative I'm using is to use mkv container crop parameters (set with mkvtoolnix). This works well but is currently only supported in Mpc-be video player.