Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have written a little bash script to extract audio from video files in batch mode.

Sound is extracted from each mp4 file to flac format using avconv (and the flac codec).

# Batch conversion of audio extraction from video


for myvid in $(find ${FOLDER_SRC} | grep mp4)
avconv -i $myvid -acodec flac "${myvid}.flac"

exit 0

It works fine, but I would like to improve it.

The script only detects mp4 format. Is it possible to recognize a video file with a command (instead of testing each video format) ?

Thank you for advices.

share|improve this question
1] If you only had videos in a folder, would you then need to grep for mp4? 2] Why are you going for flac? Aren't those files huge? – user25656 Mar 7 '13 at 15:58
1) yes you are right. in fact the script was not working because i have the file in it (not the script file itself). I can suppress this file but the question still remains :) and 2) sure ogg or mp3 would be more suitable. – riimzzai Mar 7 '13 at 16:07
up vote 2 down vote accepted

You are looking for file


 $ file -b 01--Frank_Sinatra--Close_to_You_And_More--Close_to_You.m4a 
 Audio file with ID3 version 2.4.0, contains: ISO Media, MPEG v4 system, iTunes AAC-LC

It should cover all well-known media types. If something is missing, you can add it yourself (see man magic).

share|improve this answer
thx for your answer, I tried file -b with several video files and it returns Macromedia Flash Video but also ISO Media, MPEG v4 system, version 2 for eg. So I can grep for Video or Media, and it would be suitable for my usage, but I am not 100% it contains all videos format :) – riimzzai Mar 7 '13 at 16:36
@riimzzai see update in answer – H.-Dirk Schmitt Mar 7 '13 at 16:42
okay thx (file -i works too but strangely recognize mkv as application) – riimzzai Mar 7 '13 at 16:44

To test in Bash, if $file is a video file, you could use

[[ $(ffprobe -v quiet -select_streams v:0 -show_entries stream=codec_name -print_format csv=p=0 "$file") ]] && echo $file is video

ffprobe is provided by ffmpeg package. Ubuntu 14.04 does not have ffmpeg in standard repositories, but you could add ppa:mc3man/trusty-media repository and install ffmpeg package to get the needed software. See here for details.

share|improve this answer

Your Answer


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.