Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have written this little script to batch extract audio from video using avconv. It was working fine, but strangely it doesn't work anymore (since I have changed the codec from aac to mp3).

The script is supposed to test each file in a folder. If it is a video, it should extract the audio from it.

Can somebody help me fix this?

#!/bin/sh
#
# Batch conversion of audio extraction from video
# doesnt work with mkv files (file -i *.mkv returns application/octet)

FOLDER_SRC="/home/J/Music/TestBatchConv"

for myvid in $(find ${FOLDER_SRC})
do
    if file -i $myvid | grep -q video ;
        then avconv -i $myvid -acodec libmp3lame "${myvid}.mp3"
    fi
done

exit 0
share|improve this question
1  
What is it not doing? Is it not detecting video? Is avconv not working? Any error messages? –  Atle May 10 '13 at 17:12
    
cursor blinking but no conversion nor error message... ´file -i . | grep video´ returns the video (mp4/video charshet=binary) but nothing happens... now i have changed the script with grep | mp4 but same : cursor blinking and no errors... –  riimzzai May 10 '13 at 17:40
    
Crossposted at: stackoverflow.com/questions/16486270/… –  Dennis Williamson May 10 '13 at 18:15
add comment

2 Answers

up vote 3 down vote accepted

Seems you're using an old version of file where MKV files weren't detected properly. You can use this ~/.magic file to have file identify MKV.

But actually, the most reliable way to check for valid video files would be to just run avconv and see if it fails or not. Here's what you can do:

#!/bin/bash
FOLDER_SRC="/home/J/Music/TestBatchConv"
find "$FOLDER_SRC" -type f -print0 | while IFS= read -r -d '' myvid; do
  echo "Trying to convert $myvid"
  avconv -i "$myvid" -c:a libmp3lame -q:a 4 "${myvid%.*}.mp3" 2>/dev/null || echo "$myvid is not a valid file."
done

If the file is not valid video, or there's any error, avconv will exit anyway.

Some tips and explanation:

  • There's no need to parse the output of ls or find. Simply do for myvid in "$FOLDER_SRC"/*, or for a recursive version, use the ** glob with the globstar option. Here, the find … | while … line can be replaced with:

    shopt -s globstar
    for myvid in "$FOLDER_SRC/**"; do
    
  • Don't forget to properly quote "$myvid" wherever you use it in the loop, otherwise videos with whitespace in their name break your script.

  • You can remove the original file extension by removing anything after the last dot ${myvid%.*} and replace it with .mp3.
  • You should tell avconv to use variable bitrate encoding for libmp3lame, e.g. with -q:a 4, with values ranging from 0–10, and 4 being the default. This will give you better audio quality in general.
share|improve this answer
    
thank you again, but sry all i got is 4.2.25(1)-release and /home/J/Music/TestBatchConv/ is not a valid file. like if the files test even don't happen. yes I am a noob at this ^^ –  riimzzai May 10 '13 at 18:27
    
It seems the loop is picking up your conversion folder. If you use the right glob, e.g. "$FOLDER_SRC"/**, this won't include the folder itself though. If you use the ** glob you just need to check if every item found is actually a file. Make sure it's using the right path (maybe add a echo "$myvid" in the loop to check). If you're not using the script I gave you, please show what exactly your script now looks like. –  slhck May 10 '13 at 18:32
    
I changed it exactly how you suggested (without the find) and it works. there is 2 issues not resolved : 1) the folder itself is tested (even with the "$FOLDER_SRC"/**) and 2) i don't have the avconv message anymore (and that point was mistaken me) –  riimzzai May 10 '13 at 19:06
    
and VBR is not suitable as I want to use my songs on a 2nd generation ipod (and i have a problem when encoding in VBR with libav as far as i cant navigate in the song on the ipod) –  riimzzai May 10 '13 at 19:12
    
Ah, yeah. I thought of suppressing the avconv output with 2>/dev/null. Leave out that part if you want all the info. –  slhck May 10 '13 at 19:12
show 1 more comment

For those interested here is the final working code :

#!/bin/bash
#
# Batch conversion of audio extraction from video

echo $BASH_VERSION
FOLDER_SRC="/home/J/Music/TestBatchConv"

shopt -s globstar

for myvid in "$FOLDER_SRC"/**
do
  echo "$myvid"
  avconv -i "$myvid" -acodec libmp3lame "${myvid%.*}.mp3" 2>/dev/null || echo "$myvid is not a valid file."
done
exit 0
share|improve this answer
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.