Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Something caused to damage some of my flac files, and to find out which ones I need to rerip, I would like to get a list containing only the damaged files.

How I do it now: Open a terminal in a directory, and enter: $ flac -t *.flac

The output of good files is something like:

Song1.flac: ok
Song2.flac: ok

On some of my old rips I get warnings, but the song seems to be okay:

Song3.flac: WARNING, cannot check MD5 signature since it was unset in the STREAMINFO
Song3.flac: ok 

But when an error occurs, the messages are something like:

Song4.flac: testing, 73% complete
Song4.flac: ERROR while decoding data
             state = FLAC__STREAM_DECODER_END_OF_STREAM
Song5.flac: ERROR while decoding data
            state = FLAC__STREAM_DECODER_READ_FRAME
Song6.flac: ERROR, MD5 signature mismatch                                          
Song7.flac: *** Got error code 3:FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM
Song7.flac: *** Got error code 0:FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC

Song7.flac: ERROR while decoding data
            state = FLAC__STREAM_DECODER_READ_FRAME

The FLAC stream may have been created by a more advanced encoder.  Try
  metaflac --show-vendor-tag Song7.flac
If the version number is greater than 1.2.1, this decoder is probably
not able to decode the file.  If the version number is not, the file
may be corrupted, or you may have found a bug.  In this case please
submit a bug report to
    http://sourceforge.net/bugs/?func=addbug&group_id=13478
Make sure to use the "Monitor" feature to monitor the bug status.

My question is:

  • How can I filter the output to only show files with warnings (output contains WARNING) or errors (output contains ERROR)?

I think it might be doable with grep, but I don't know how to give the output of the flac -t command to grep. With flac -c

  • And if that works, how can I check all subdirs at once?

My music collection is sorted into many subdirs and going to every dir and run the command would be laborious.

Bonus points if it could output the path of the corrupt file, not only the name :)

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Using bash 4's globstar, this will recursively find all flac files from the current directory, and output the error code and filename for the ones with errors and warnings.

#!/usr/bin/env bash
shopt -s globstar

for file in ./**/*.flac; do
    flac -wst "$file" 2>/dev/null || printf '%3d %s\n' "$?" "$file"
done

The manual does not document what error codes it exits with for different types of errors, therefore I added the error code in the output, maybe you can see a pattern from it.

share|improve this answer
    
That seems to be the solution. Thank you! Could you please explain what the fourth line in your code does? It seems the error code is alway 1 both for the warnings and errors –  shaddow Jul 23 '12 at 19:36
    
You mean for file in ./**/*.flac; do? When globstar is enabled with shopt -s globstar, ** will match all files and directories recursively. See Pattern Matching –  geirha Jul 23 '12 at 19:42
    
No, I meant the following 5th line, sorry, I didn't count the empty line ;) –  shaddow Jul 23 '12 at 19:54
    
@shaddowy2, cmd1 || cmd2 runs cmd1, and if cmd1 fails (returns exit status != 0), it runs cmd2. If cmd1 succeeds (returns exit status == 0), it does not run cmd2. So flac ... || printf ... is a shorthand for if ! flac ...; then printf...; fi –  geirha Jul 23 '12 at 19:57
    
Thank you for your explanation. –  shaddow Jul 23 '12 at 20:04
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.