505

How can I strip the audio track out of a video file with FFmpeg?

Brad
  • 5,686
  • 8
  • 52
  • 83
Rami Dabain
  • 5,297
  • 4
  • 17
  • 10

5 Answers5

721

You remove audio by using the -an flag:

input_file=example.mkv
output_file=example-nosound.mkv

ffmpeg -i $input_file -c copy -an $output_file

This ffmpeg flag is documented here.

Iulian Onofrei
  • 312
  • 5
  • 17
Martin Beckett
  • 9,753
  • 1
  • 27
  • 28
  • 4
    I'm a `bash` and `ffmpeg` newbie but I put this answer together with some other pieces to create `function ffsilent { ffmpeg -i $1 -c copy -an "$1-nosound.${1#*.}" }` which you can use in your profile to quickly create a silent version of any video file. – Aaron Dec 16 '19 at 15:18
  • 9
    @Aaron nice, but should be `function ffsilent { ffmpeg -i "$1" -c copy -an "${1%.*}-nosound.${1#*.}" }` or you'll end up with "file.mp4-nosound.mp4" when using it on "file.mp4". – Alexander Revo Jul 07 '20 at 08:52
  • 1
    This doesn't carry over GPS coordinates. – Donny V Dec 08 '20 at 16:43
  • Using `-c copy -an` works on most video files but it won't strip audio from SWF (Shockwave Flash) files. The solution is to shorten it to just `-an` then it works. – bat_cmd Apr 04 '21 at 00:18
  • Are you sure that this avoids re-encoding the video? – rlittles Jun 13 '22 at 00:06
  • 3
    @rlittles Yes, `-c copy` always avoids re-encoding, If it can't it will fail with an error. – FFmpegEnthusiast Jul 10 '22 at 00:35
  • 1
    Be sure to put the `-an` flag before `$output_file` – Jan Apr 12 '23 at 14:11
132

You probably don't want to reencode the video (a slow and lossy process), so try:

input_file=example.mkv
output_file=example-nosound.mkv

ffmpeg -i $input_file -vcodec copy -an $output_file

(n.b. some Linux distributions now come with the avconv fork of ffmpeg)

Bruno Bronosky
  • 1,885
  • 1
  • 20
  • 26
John Mellor
  • 1,611
  • 1
  • 11
  • 6
  • 2
    This didn't make any difference to me compared to the accepted solution. – nidi Dec 29 '17 at 00:49
  • 7
    vcodec is an alias for `-c:v`, so specifically it'd copy the video stream only. The only data you're preventing with this would be subtitles, metadata, etc from what I can see. – Rogue Mar 08 '18 at 15:48
  • 3
    In other words, this solution can conceivably lose more information than the accepted solution. – Alex Feb 25 '20 at 15:12
  • 2
    We can call this "only video" solution :+1: – Vladimir Vukanac Nov 01 '21 at 10:44
  • 2
    I agree: This here is the "copy video only" solution, whereas the accepted answer is the "copy everything but audio" solution. – porg Nov 09 '22 at 19:03
12
avconv -i [input_file] -vcodec copy -an [output_file]

If you cannot install ffmpeg because of existing of avconv try that .

Abdennour TOUMI
  • 249
  • 2
  • 7
3

I put together a short code snippet that automates the process of removing audio from videos files for a whole directory that contains video files:

FILES=/{videos_dir}/*
output_dir=/{no_audio_dir}
for input_file in $FILES
do
  file_name=$(basename $input_file)
  output_file="$output_dir/$file_name"
  ffmpeg -i $input_file -c copy -an $output_file
done

I hope this one helps!

apolak
  • 39
  • 1
-1

I have taken @apolak's answer and turned it in to a recursive loop for all folders underneath the input folder. It will retain the directory layout of the input folder, and you can set a max-depth for it to recurse through. The output directory must not be a child of the input directory or it will error, to stop accidental infinite recursion. It should also be fine with spaces in filenames and paths.

NOTE: all files within the input directory will be attempted to be processed, so make sure they're all video files.

#!/bin/bash

process_files() {
    local current_dir="$1"
    local output_dir="$2"
    local max_depth="$3"
    local depth="${4:-0}" # Set default value of 0 if $4 is not set
    if [ "$depth" -gt "$max_depth" ]; then
        return
    fi
    # Check if output directory is a subdirectory of the input directory and error
    # This should stop accidental recursive loops
    if [[ "$output_dir" == "$current_dir"* ]]; then
        echo "Error: Output directory is a subdirectory of the input directory"
        exit 1
    fi
    mkdir -p "$output_dir"
    for input_file in "$current_dir"/*
    do
        if [ -d "$input_file" ]; then
            # If the input file is a directory, recurse into it
            process_files "$input_file" "$output_dir/$(basename "$input_file")" "$max_depth" "$((depth+1))"
        elif [ -f "$input_file" ]; then
            # If the input file is a regular file, process it
            local file_name=$(basename "$input_file")
            local output_file="$output_dir/$file_name"
            ffmpeg -i "$input_file" -c copy -an "$output_file"
        fi
    done
}

# Call function with input and output directories and maximum depth
process_files "/Volumes/Storage/ORIGINAL" "/Volumes/Storage/MUTED" 2 # Set the maximum recursion depth to 2
PaulSkinner
  • 149
  • 3