Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mac shell script to extract audio from video files

Tags:

shell

macos

I'm trying to write a shell script that will extract the audio from all the movie files in a folder.

audio_extracter.sh

for f in *; do
    if  [ "$f" != "audio_extracter.sh" ]; then
        /usr/bin/avconvert --source "$f" --output */Converted/"$f" --audioTrack -af aac
        echo "$f converted"
    else
        echo "problems"
        exit 1
    fi
done

It's spitting back avconvert is a command line application that will transcode a source or group of sources to create a destination file output...

Also, if possible the script should find out the format of the audio in the video and extract it as such, without converting it to a different different format (at the moment I think I'm forcing it to convert to AAC).

Any thoughts on how to make this work?

like image 885
Eric Avatar asked Feb 22 '26 08:02

Eric


1 Answers

-acodec copy extracts audio without re-encoding in ffmpeg:

for f in *.mkv; do ffmpeg -i "$f" -acodec copy "${f%mkv}aac"; done

You can see the formats of the audio streams with ffmpeg -i input.mkv.

like image 193
Lri Avatar answered Feb 25 '26 15:02

Lri