Skip to content

Instantly share code, notes, and snippets.

@samy
Last active August 29, 2015 14:17
Show Gist options
  • Save samy/47e1dd55bccffab23d85 to your computer and use it in GitHub Desktop.
Save samy/47e1dd55bccffab23d85 to your computer and use it in GitHub Desktop.
How to detect truncated MP3 files
# Retrieve MP3s list
find . -type f -iname "*.mp3" -print0 | while IFS= read -r -d $'\0' line; do
# Use sox to extract a MP3 which ends at 0 seconds of the current file
sox "$line" temp.mp3 reverse trim 0 reverse
# We retrieve original file and copy durations, as integers
ORIGINALDURATION=`sox --i -D "$line"`
ORIGINALDURATION=`printf %.0f $ORIGINALDURATION`
COPYDURATION=`sox --i -D temp.mp3`
COPYDURATION=`printf %.0f $COPYDURATION`
# A comparison is made between the two values
DELTA=` expr $ORIGINALDURATION - $COPYDURATION`
if [ $DELTA -gt 10 ]
then
# If the diff is more than 10 seconds, we rename the file
echo 'ERROR : '.$line
linenew=`echo "$line" | sed 's/.mp3/.fail/'`
mv "$line" "$linenew"
rm temp.mp3
else
echo 'OK : '.$line
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment