David Bosch - Jan 12th, 2022
In this lesson I will summarize the most common ways of removing audio tracks from video files using FFMPEG. As always happens with FFMPEG, there's no a one fits-all approach and there are several ways of striping audio from video files. I tried to include the most common situations. If you miss your particular use case, drop me a message.
You can download the video files I will use for the examples:
Remove audio from video with FFMPEG
Remove audio from video
In some cases, you will need to remove one or more audio streams from a video file. There are multiple ways to achieve this with FFMPEG:
Remove all audio streams from a video file with -an
There is a specific FFMPEG parameter to filter out all audio streams called -an
.
This parameter disables audio recording i.e. automatic selection or mapping of any audio stream
in the output file.
ffmpeg \
-i video1.mp4 \
-an \
-c copy \
-y output.mp4
The output.mp4
won't include any audio stream.
Remove all audio streams from a video file with map
In this example, I only keep the video streams, dismissing any other tracks (audio tracks, subtitles, data, etc). The final result is exactly the same the example above.
ffmpeg \
-i video1.mp4 \
-map 0:v \
-c copy \
-y output.mp4
The mapping refers only to -map 0:v
meaning the video streams from input #0.
Remove a particular audio track
If the video file has more than one audio track, you can selectively remove any track.
For example, the following command removes the first audio track (0:a:0
), but keeps the rest:
In the schema above, the Italian (0:a:1
) and Spanish (0:a:2
) audio tracks are copied to the output file,
but the English track (0:a:0
) is not copied. Note that in the output file, the audio tracks are reassigned starting from 0.
The FFMPEG command to achieve this is:
ffmpeg \
-i video1.mp4 \
-map 0 -map -0:a:0 \
-c copy \
-y output.mp4
It maps/adds all streams (-map 0
) but substracts the first audio track (-map -0:a:0
).
The minus sign before 0:a:0
means substracting.
Remove audio from video and replace it with a silence audio track
In some cases you need to keep an audio stream in your video file to make it compatible with legacy systems or existing workflows. In this cases, the solution is to replace the audio track with a silent audio.
ffmpeg \
-i video1.mp4 -f lavfi -i anullsrc \
-c:v copy \
-shortest \
-map 0:v -map 1:a \
-y output.mp4
The -f lavfi -i anullsrc
generates a virtual audio source with silence with infinite length.
That is why it's important to specify -shortest
to limit the output duration to the video stream duration.
If not, an infinite output file would be created.
Remove audio from a MKV file
MKV is a very versatile format that can include video and audio streams in many encoding formats. This makes them a very easy and popular format to use in certain situations.
All examples above should work perfectly well in MKV just specifying the proper .mkv file in the FFMPEG input, for example:
ffmpeg \
-i video1.mkv \
-an \
-c copy \
-y output.mkv
Remove audio from a MP4 file
MP4 probably became the most popular video format in the Internet and it's defacto standard in most websites and platforms, like Youtube, Vimeo and others.
There are some MP4 video players that require specific parameters to correctly play the video, but others are more flexible and can play almost anything. In the first group you find MacOS Quicktime, in the second, VLC player.
All examples above are written to be used with MP4 video files, but if at some point you have any problem playing them with Quicktime, you should add these parameters:
ffmpeg \
-i video1.mkv \
-an \
-c copy \
-pix_fmt yuv420p -vcodec libx264 -acodec aac
-y output.mkv
These extra parameters (-pix_fmt yuv420p -vcodec libx264 -acodec aac
) forces FFMPEG to use the pixel format and
the audio and video codecs that Quicktime can understand.
Final thoughts on removing audio from video using FFMPEG
In your day to day video operations you will face situations when you must remove one or more audio tracks from a file, replace them with a silent track or simply striping the audio. The above examples will do the trick for most of this situations.
Sources
- https://ottverse.com/add-remove-extract-audio-from-video-using-ffmpeg/
- https://dfarq.homeip.net/ffmpeg-can-remove-audio-heres-how/
- https://superuser.com/questions/268985/remove-audio-from-video-file-with-ffmpeg
Published on January 12th, 2022