This repository has no description
1.2 kB
47 lines
1#!/usr/bin/env bash
2set -euo pipefail
3
4# Recording duration in seconds (default: 5)
5DURATION=${1:-5}
6
7# Temporary file for audio
8TEMP_AUDIO=$(mktemp /tmp/whatsong-XXXXXX.wav)
9trap 'rm -f "$TEMP_AUDIO"' EXIT
10
11echo "Listening for $DURATION seconds..." >&2
12
13# Record audio from default microphone
14# songrec needs specific format: 16kHz mono
15rec -q -r 16000 -c 1 -b 16 "$TEMP_AUDIO" trim 0 "$DURATION" 2>/dev/null || true
16
17if [ ! -s "$TEMP_AUDIO" ]; then
18 echo "Error: No audio recorded" >&2
19 exit 1
20fi
21
22echo "Identifying song..." >&2
23
24# Run songrec and get JSON output
25RESULT=$(songrec recognize --json "$TEMP_AUDIO" 2>/dev/null)
26
27if [ -z "$RESULT" ] || [ "$RESULT" = "null" ]; then
28 echo "Could not identify song" >&2
29 exit 1
30fi
31
32# Extract artist and title
33ARTIST=$(echo "$RESULT" | jq -r '.track.subtitle // empty')
34TITLE=$(echo "$RESULT" | jq -r '.track.title // empty')
35
36if [ -z "$TITLE" ]; then
37 echo "Could not identify song" >&2
38 exit 1
39fi
40
41echo "Found: $ARTIST - $TITLE" >&2
42
43# Search YouTube and get first result
44SEARCH_QUERY="$ARTIST $TITLE"
45echo "Searching YouTube..." >&2
46
47yt-dlp --dump-json "ytsearch1:$SEARCH_QUERY" 2>/dev/null | jq -r '.webpage_url'