automatically extract audio track and name file from video with avconv

445 views Asked by At

I'm writing a bash script to automatically extract audio track from a video file using avconv. This script should handle any video format and only extract the audio track without keeping the original codec conversion. For example:

extract_audio name.flv

where the audio track of the flv file is an mp3 should produce a file named name.mp3, but if it contains and aac track it should produce aumatically name.aac

For now my script looks like this:

#!/bin/bash
dryname=${1%.*} #get remove video extention
avconv -i "$1" -vn -acodec copy "${dryname}.${audio_extension}"

The only part that I miss is how to get the codec used in the audio track of the video file. I'm sure it's possible to do it with an avconv command but I can't figure it out from the help page.

2

There are 2 answers

4
ajaaskel On BEST ANSWER
audio_extension=$(avconv -i "$1" 2>&1 | grep -io "audio:...."|cut -d ' ' -f2)
0
lucky1928 On

You can run below command firstly to check codec info, then do extract with proper suffix.

$ avconv -i test.mp4 2>&1 |grep Audio
    Stream #0.0(eng): Audio: aac, 44100 Hz, stereo, fltp, 62 kb/s (default)