Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Having an MPEG4 (MP4 container, H264 AVC video, AAC audio) video clip downloaded from YouTube, how to separate its audio channel into an independent audio (without de&re-compressing the audio preferably) file with free Linux tools?

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

If you know there is only 1 audio stream and that it is aac, then you only need this:

ffmpeg -acodec copy -i "movie.mp4" -y "audio.aac"

If you want to script the first audio stream ID and codec-type/file-extension, you can use this:

eval $(ffmpeg -i "movie.mp4" 2>&1 |awk '/Stream.*Audio/{print "stream=" $2 ";codec=" $4}' |head -n1)
stream=${stream:1}; stream=${stream/:/}
extn=${codec/,/}; [[ $extn == "aac" ]] || 
    { echo "You need to manually set the fiie extension. Codec type is: $codec"; exit 1; } 

ffmpeg -map $stream -acodec copy -i "movie.mp4" "audio.$extn"
share|improve this answer
    
eval: 1: Syntax error: "(" unexpected –  Ivan Apr 11 '12 at 19:58
1  
Perhaps ffmpeg -i in not outputing a single line which matches /Stream.*Audio/ line... If there is more than one, then to grab the first one, you need eval $(ffmpeg -i "movie.mp4" 2>&1 |awk '/Stream.*Audio/{print "stream=" $2 ";codec=" $4} |head -n1) .... Otherwise just experiment with ffmpeg -i and see what it needs, or for a one-off, manually enter the correct filetype... –  Peter.O Apr 11 '12 at 20:13
    
By the way, do you happen to know a similar trick for FLV files? ffmpeg says "Unable to find a suitable output format for 'audioFile'". Seems it thinks it's webm. –  Ivan Jun 20 '12 at 0:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.