Cut and convert audio clips for recognition with ffmpeg
The ffmpeg commands you need to prepare audio for AudD: cut a 12-second clip at a given time, take the audio out of a video, get a file under the standard endpoint's 10 MB cap, record a few seconds from a stream, and split a long file, with the file size each setting produces.
The standard endpoint analyzes up to the first 12 seconds of audio, and it accepts uploaded files up to 10 MB. So if the song you want is in the middle of a long recording, or the file is too big, cut a short clip with ffmpeg and send the clip instead. Here are the commands you’ll need. The file sizes on this page are what these commands produce with ffmpeg 6.1.
Cut a 12-second clip at a given time
ffmpeg -ss 00:01:15 -i input.mp4 -t 12 -vn clip.wav
-ss 00:01:15before-itells ffmpeg where to start: 1 minute 15 seconds into the file. You can also write1:15or75. Since the audio is encoded again, ffmpeg decodes and drops everything before that point, so the clip starts where you asked.-t 12stops writing the output after 12 seconds.-vnleaves the video out, so this works the same for a video and an audio file.- The extension of the output file picks the format: here, uncompressed WAV.
The clip is 2.3 MB if the video has 48 kHz stereo sound, so it’s well under the 10 MB cap. You can send it with the AudD CLI, audd recognize clip.wav, or upload it in the file parameter:
curl https://api.audd.io/ -F api_token='your-api-token' -F [email protected]
If you want a smaller file, encode the clip as MP3. At 128 kbps, the same 12 seconds take 193 KB:
ffmpeg -ss 00:01:15 -i input.mp4 -t 12 -vn -c:a libmp3lame -b:a 128k clip.mp3
Tip. If you use the AudD CLI, you don’t have to cut the clip yourself.
audd recognize input.mp4 --at 1:15will send a 12-second clip from 1:15, and you can change its length with--duration. The CLI uses ffmpeg for that, so you’ll need ffmpeg installed.
The result’s timecode is the time in the recognized song when your clip is played, not a time in your file. If you cut the clip at 1:15 and the result says 00:56, the song was playing at 1:15 of your file, 56 seconds into the song. For all the time fields, see Timestamps and offsets.
When the clip comes out empty
If -ss is past the end of the file, ffmpeg won’t exit with an error. It prints Output file is empty, nothing was encoded and writes a file with no audio in it (78 bytes for a WAV). Please check the length of the clip before sending it:
ffprobe -v error -show_entries format=duration,size -of default=noprint_wrappers=1 clip.wav
duration=12.000000
size=2304078
If the clip is empty, you’ll see duration=N/A instead. In this case, pick an earlier time: you can check how long the input file is with the same command.
Take the audio out of a video
Before you take the audio out, check what kind of audio the video has:
ffprobe -v error -select_streams a -show_entries stream=index,codec_name,channels,sample_rate -of compact=p=0 input.mp4
index=1|codec_name=aac|sample_rate=48000|channels=2
If it’s AAC, you can copy it into an .m4a file. With -c:a copy, the audio is copied as it is, without encoding it again, so it’s fast:
ffmpeg -i input.mp4 -vn -c:a copy audio.m4a
If the audio is in another format, ffmpeg may refuse to copy it into .m4a. For example, for Opus audio from a WebM file, it says Could not find tag for codec opus in stream #0, codec not currently supported in container. In that case, encode the audio instead:
ffmpeg -i input.webm -vn -c:a libmp3lame -b:a 128k audio.mp3
At 128 kbps, the audio is about 1 MB per minute. If the audio track starts at the same time as the picture, the times in the results are times in the video. To check it, add start_time to the stream= list of the ffprobe command above.
If the video has more than one audio track, you can pick one with -map: -map 0:a:0 is the first audio track, and -map 0:a:1 is the second. If it has no audio at all, ffmpeg will stop with Output file does not contain any stream.
You don’t have to do this if you use the enterprise endpoint: it accepts MP4, AVI, MOV, MKV, and WebM files, and extracts the audio track on our side. Extracting the audio yourself only makes the upload smaller.
Get a file under the 10 MB cap
In most cases, you’ll just need a shorter clip, since the standard endpoint analyzes up to the first 12 seconds anyway. If you want every song in a long file, send it to the enterprise endpoint instead: it doesn’t have the 10 MB cap, and we count 1 request per 12 seconds of audio.
Here’s what the channels, the sample rate and the bitrate do to the size, for a 44.1 kHz stereo source:
| Output | Options | 12 seconds | 10 minutes |
|---|---|---|---|
| WAV, stereo | (none) | 2.1 MB | 105.8 MB |
| WAV, mono | -ac 1 | 1.1 MB | 52.9 MB |
| WAV, mono, 22.05 kHz | -ac 1 -ar 22050 | 529 KB | 26.5 MB |
| MP3, 192 kbps | -c:a libmp3lame -b:a 192k | 290 KB | 14.4 MB |
| MP3, 128 kbps | -c:a libmp3lame -b:a 128k | 193 KB | 9.6 MB |
| MP3, 128 kbps, mono | -ac 1 -c:a libmp3lame -b:a 128k | 193 KB | 9.6 MB |
| AAC, 128 kbps | -c:a aac -b:a 128k (.m4a) | 195 KB | 9.8 MB |
The higher the sample rate, the larger the WAV file, and a stereo WAV is twice as large as a mono one: at 44.1 kHz in stereo, it passes 10 MB in less than a minute. MP3 and AAC files grow with the bitrate instead. So -ac 1 alone doesn’t make an MP3 file smaller: at 128 kbps, a mono file is the same size as a stereo one.
These settings change the size of the file. For what they do to recognition, see Audio formats and quality: you don’t need to upsample or convert to lossless, and mono is fine.
Record a few seconds from a stream
ffmpeg -i https://npr-ice.streamguys1.com/live.mp3 -t 12 -c copy stream-clip.mp3
ffmpeg records in real time, so the command takes about 12 seconds. With -c copy, the stream’s MP3 is saved as it is. If the stream isn’t MP3, you can write a WAV file instead: ffmpeg -i <stream URL> -t 12 -vn stream-clip.wav.
If you want to identify every song played on a stream, you don’t need to record it. Add the stream to AudD, and we’ll send you the recognition results in real time (see Webhook callbacks vs longpoll). To record from a microphone, you can use audd listen: it records with ffmpeg (or sox), and --device picks the input.
Split a long file
Usually, you don’t have to: the enterprise endpoint takes the whole file and splits it into 12-second chunks on our side. Split the file yourself when you need the pieces, for example to upload them one by one:
ffmpeg -i long-recording.mp3 -f segment -segment_time 600 -segment_list parts.csv -c copy part%03d.mp3
This writes 10-minute files, part000.mp3, part001.mp3 and so on, and a list with the start and end of each part in seconds:
part000.mp3,0.000000,600.006531
part001.mp3,600.006531,1200.013061
part002.mp3,1200.013061,1500.029388
To get a position in the original file, add the part’s start time to the position in the part. With -c copy, the parts are close to 600 seconds, but not exact, so please use the times from the list. If you split .m4a files, add -reset_timestamps 1, so that each part starts at zero like a separate file.
If you use -segment_time 12, you’ll get 12-second clips for the standard endpoint. Each clip is a request, so the clips of a whole file cost about as many requests as an enterprise scan of it, and the enterprise endpoint will also tell you where each song starts and ends.
Cut and send a clip from Python
If you’re writing code, you can skip the temporary file. ffmpeg will write the clip to its standard output, and you can pass the bytes to the Python SDK (pip install audd):
import subprocess
from audd import AudD
def clip_at(path, start, seconds=12):
"""Cut `seconds` of audio starting at `start` and return them as MP3 bytes."""
return subprocess.run(
["ffmpeg", "-v", "error", "-ss", str(start), "-i", path, "-t", str(seconds),
"-vn", "-c:a", "libmp3lame", "-b:a", "128k", "-f", "mp3", "pipe:1"],
capture_output=True, check=True,
).stdout
audd = AudD("your-api-token") # get your own token at dashboard.audd.io
result = audd.recognize(clip_at("input.mp4", 75))
if result is None:
print("No match: try another moment of the file.")
else:
print(f"{result.artist} - {result.title}, {result.timecode} into the song")
Here, -f mp3 names the format (there’s no file extension to guess it from), and pipe:1 means the standard output. If there are no matches, you’ll get None: try a clip from another moment of the file.
Related
Reading this as an AI agent? The raw Markdown is at recipes/ffmpeg-clips-for-recognition.md, and the full index is /resources/llms.txt.
