Advertisement
Gooningbobby

Untitled

Apr 27th, 2025
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. #!/data/data/com.termux/files/usr/bin/bash
  2.  
  3. # === Config ===
  4. API_KEY="YOUR_API_KEY_HERE"
  5.  
  6. # Folders
  7. BASE_DIR="$HOME/.shortcuts/song_recognizer"
  8. RECORDINGS_DIR="$BASE_DIR/recordings"
  9. mkdir -p "$RECORDINGS_DIR"
  10.  
  11. # Files
  12. TEMP_FILE="$RECORDINGS_DIR/recording_tmp.wav"
  13. FINAL_FILE="$RECORDINGS_DIR/record.wav"
  14. HQ_FILE="$RECORDINGS_DIR/record_high.wav"
  15. LOG_FILE="$BASE_DIR/song_log.txt"
  16. RESPONSE_FILE="$BASE_DIR/response.json"
  17. REQUIRED_JSON_FILE="/sdcard/required-res.json"
  18. HISTORY_JSON_FILE="/sdcard/song_history.json"
  19.  
  20. # === Record Audio ===
  21. termux-microphone-record -l 15 -f "$TEMP_FILE"
  22. sleep 15
  23. termux-microphone-record -q
  24.  
  25. # === Convert Audio to High Quality ===
  26. ffmpeg -y -i "$TEMP_FILE" -ar 44100 -ac 2 -sample_fmt s16 "$HQ_FILE"
  27.  
  28. # Finalize file
  29. mv -f "$HQ_FILE" "$FINAL_FILE"
  30. rm -f "$TEMP_FILE"
  31.  
  32. # === Send to Shazam API ===
  33. response=$(curl -s --request POST \
  34. --url 'https://shazam-song-recognition-api.p.rapidapi.com/recognize/file' \
  35. --header "X-RapidAPI-Key: $API_KEY" \
  36. --header 'X-RapidAPI-Host: shazam-song-recognition-api.p.rapidapi.com' \
  37. --header 'Content-Type: multipart/form-data' \
  38. --form "upload=@$FINAL_FILE")
  39.  
  40. # Save raw API response
  41. echo "$response" > "$RESPONSE_FILE"
  42.  
  43. # === Extract Info ===
  44. title=$(echo "$response" | jq -r '.track.title // "Unknown Title"')
  45. artist=$(echo "$response" | jq -r '.track.subtitle // "Unknown Artist"')
  46. album=$(echo "$response" | jq -r '.track.sections[]? | select(.type == "SONG") | .metadata[]? | select(.title == "Album") | .text' | head -n1)
  47. album=${album:-"Unknown Album"}
  48. timestamp=$(date '+%Y-%m-%d %H:%M:%S')
  49.  
  50. # Build YouTube Music URL
  51. escaped_query=$(echo "$title $artist" | jq -sRr @uri)
  52. ytm_url="https://music.youtube.com/search?q=${escaped_query}"
  53.  
  54. # === Output to Terminal ===
  55. echo "$title - $artist ($album)"
  56. echo "$ytm_url"
  57.  
  58. # === Save Required JSON ===
  59. jq -n \
  60. --arg title "$title" \
  61. --arg artist "$artist" \
  62. --arg album "$album" \
  63. --arg timestamp "$timestamp" \
  64. --arg youtube_music_url "$ytm_url" \
  65. '{
  66. title: $title,
  67. artist: $artist,
  68. album: $album,
  69. timestamp: $timestamp,
  70. youtube_music_url: $youtube_music_url
  71. }' > "$REQUIRED_JSON_FILE"
  72.  
  73. # === Append to Text Log ===
  74. {
  75. echo "# [$timestamp] $title - $artist (Album: $album)"
  76. echo "# $ytm_url"
  77. echo "#"
  78. } >> "$LOG_FILE"
  79.  
  80. # === Update History JSON ===
  81. if [ ! -f "$HISTORY_JSON_FILE" ]; then
  82. echo '[]' > "$HISTORY_JSON_FILE"
  83. fi
  84.  
  85. tmpfile=$(mktemp)
  86. jq --arg title "$title" \
  87. --arg artist "$artist" \
  88. --arg album "$album" \
  89. --arg timestamp "$timestamp" \
  90. --arg youtube_music_url "$ytm_url" \
  91. '. += [{
  92. "title": $title,
  93. "artist": $artist,
  94. "album": $album,
  95. "timestamp": $timestamp,
  96. "youtube_music_url": $youtube_music_url
  97. }]' "$HISTORY_JSON_FILE" > "$tmpfile" && mv "$tmpfile" "$HISTORY_JSON_FILE"
  98.  
  99. # === Send Notification ===
  100. termux-notification \
  101. --title "Now Playing" \
  102. --content "$title - $artist [$album]" \
  103. --priority high \
  104. --button1 "Open YouTube" \
  105. --button1-action "am start -a android.intent.action.VIEW -d \"$ytm_url\""
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement