title: find the timestamp of a sound sample of an mp3 with linux or python
body: <p>I am slowly working on a project which where it would be very useful if the computer could find <em>where</em> in an mp3 file a certain sample occurs. I would restrict this problem to meaning a fairly exact snippet of the audio, not just for example the chorus in a song on a different recording by the same band where it would become more some kind of machine learning problem. Am thinking if it has no noise added and comes from the same file, it should somehow be possible to locate the time at which it occurs without machine learning, just like grep can find the lines in a textfile where a word occurs.</p>
  <p>In case you don't have an mp3 lying around, can set up the problem with some music available on the net which is in the public domain, so nobody complains:</p>
  <pre><code>curl https://web.archive.org/web/20041019004300/http://www.navyband.navy.mil/anthems/ANTHEMS/United%20Kingdom.mp3 --output godsavethequeen.mp3
  </code></pre>
  <p>It's a minute long:</p>
  <pre><code>exiftool godsavethequeen.mp3 | grep Duration
  Duration                        : 0:01:03 (approx)
  </code></pre>
  <p>Now cut out a bit between 30 and 33 seconds (the bit which goes la la la la..):</p>
  <pre><code>ffmpeg -ss 30 -to 33 -i godsavethequeen.mp3 gstq_sample.mp3
  </code></pre>
  <p>both files in the folder:</p>
  <pre><code>$ ls -la
  -rw-r--r-- 1 cardamom cardamom   48736 Jun 23 00:08 gstq_sample.mp3
  -rw-r--r-- 1 cardamom cardamom 1007055 Jun 22 23:57 godsavethequeen.mp3
  </code></pre>
  <p>For some reason exiftool seems to overestimate the duration of the sample:</p>
  <pre><code>$ exiftool gstq_sample.mp3 | grep Duration
  Duration                        : 6.09 s (approx)
  </code></pre>
  <p>..but I suppose it's only approximate like it tells you.</p>
  <p>This is what am after:</p>
  <pre><code>$ findsoundsample gstq_sample.mp3 godsavethequeen.mp3
  start 30 end 33
  </code></pre>
  <p>Am happy if it is a bash script or a python solution, even using some kind of python library. Sometimes if you use the wrong tool, the solution might work but look horrible, so whichever tool is more suitable. This is a one minute mp3, have not thought yet about performance just about getting it done at all, but would like some scalability, eg find ten seconds somewhere in half an hour.</p>
  <p>Have been looking at the following resources as I try to solve this myself:</p>
  <p><a href="https://stackoverflow.com/questions/38075577/how-to-recognize-a-music-sample-using-python-and-gracenote">How to recognize a music sample using Python and Gracenote?</a></p>
  <p><a href="https://github.com/craigfrancis/audio-detect" rel="noreferrer">https://github.com/craigfrancis/audio-detect</a></p>
  <p><a href="https://madmom.readthedocs.io/en/latest/introduction.html" rel="noreferrer">https://madmom.readthedocs.io/en/latest/introduction.html</a></p>
  <p><a href="https://stackoverflow.com/questions/2060628/reading-wav-files-in-python">Reading *.wav files in Python</a></p>
  <p><a href="https://github.com/aubio/aubio" rel="noreferrer">https://github.com/aubio/aubio</a></p>
  <p><a href="https://aubio.org/manpages/latest/aubioonset.1.html" rel="noreferrer"><code>aubionset</code></a> is a good candidate</p>
  <p><a href="https://willdrevo.com/fingerprinting-and-audio-recognition-with-python/" rel="noreferrer">https://willdrevo.com/fingerprinting-and-audio-recognition-with-python/</a></p>