How to compare two lossless audio files?

I have an M4A file which is also converted to a FLAC file. I'd like to see if the conversion is lossless, namely, whether the output to pcm from M4A is exactly identical to the one from FLAC decoding.

I assume there's a way to use FFmpeg or Libav to produce some "raw" output and compare them?


Solution 1:

I'd try converting them both to WAV and comparing their checksums.

ffmpeg -i file1.m4a file1.wav
ffmpeg -i file2.flac file2.wav
md5sum file1.wav
md5sum file2.wav
rm file?.wav

Compare the md5s produced. If they match, congratulations! Your files contain the same data. If they don't match, post the output of those commands here, and I'll look. Potentially there is a bitrate difference or something (there ought not to be... but there may be, I don't know.)

Note that the ffmpegs will generate comparatively large intermediate files.

Solution 2:

You can use the hash muxer to generate a checksum of the decoded media. No need to convert files, and it is unaffected by metadata or other factors that can cause a standalone sum tool to report false differences.

Example to compare WAV → FLAC. Because FLAC is lossless the hashes should be the same:

$ ffmpeg -loglevel error -i input.wav output.flac

$ ffmpeg -loglevel error -i input.wav -map 0 -f hash -
  SHA256=c1acb198952f5c341190ffb62eeafe4f10c8f48c67a188e25087471a74eaa957

$ ffmpeg -loglevel error -i output.flac -map 0 -f hash -
  SHA256=c1acb198952f5c341190ffb62eeafe4f10c8f48c67a188e25087471a74eaa957
  • There are many available hash algorithms to choose from. Some are faster than others. You can select an algorithm with the -hash option, such as -hash md5.

  • -map 0 is used in the examples to include all streams into the checksum. Without it the default stream selection behavior will only choose one stream per stream type. If you want to exclude/include specific streams then do so with the -map option with stream specifiers. For example, to exclude all video use negative mapping with -map -0:v, or to only include audio use -map 0:a, or to only include the third audio stream use -map 0:a:2.

  • The streamhash muxer is similar to hash, but it will output a hash per stream, such as one for video and one for audio. Again, it also will use the default stream selection behavior unless you add -map.

  • If you want to compare each individual frame/packet then use the framehash muxer.