如何用youtube-dl读取txt列表批量搜索并下载YouTube音频
Got it, let's break this down into actionable steps. First, a quick critical note: youtube-dl is no longer actively maintained, so I’d strongly recommend using its drop-in replacement yt-dlp instead—it supports all the same core functionality plus fixes for modern YouTube changes. That said, I’ll include notes for both tools where relevant.
Prerequisites
First, make sure you have these installed:
yt-dlp(oryoutube-dl): Grab it via your package manager (e.g.,brew install yt-dlpon macOS,sudo apt install yt-dlpon Debian/Ubuntu) or download directly from its official distribution channels.ffmpeg: Required to extract and convert audio formats. Install viabrew install ffmpeg,sudo apt install ffmpeg, or its official installer for Windows.
Batch Scripts for Different OSes
The core idea is to loop through each word in your list.txt file, use yt-dlp to search YouTube for that word, and download the best available audio for the top search result.
Linux/macOS (Bash Script)
Create a file named download_audio.sh with this content:
#!/bin/bash # Loop through each line in list.txt, skip empty lines while IFS= read -r word; do [[ -z "$word" ]] && continue # Download top search result as highest-quality MP3 yt-dlp \ -x \ # Extract audio only (discard video) --audio-format mp3 \ # Output file format --audio-quality 0 \ # Highest quality (0-9 scale, 0 = best) -o "%(title)s - ${word}.%(ext)s" \ # Name file with your target word "ytsearch1:$word" # Search YouTube, grab the top result done < list.txt
Then make it executable and run:
chmod +x download_audio.sh ./download_audio.sh
Windows (Command Prompt Batch Script)
Create a file named download_audio.bat with this content:
@echo off setlocal enabledelayedexpansion for /f "tokens=* delims=" %%w in (list.txt) do ( if not "%%w"=="" ( yt-dlp ^ -x ^ --audio-format mp3 ^ --audio-quality 0 ^ -o "%%(title)s - %%w.%%(ext)s" ^ "ytsearch1:%%w" ) )
Double-click the .bat file or run it directly from Command Prompt.
Customization Options
Tweak these parameters to fit your needs:
- Get multiple results per word: Replace
ytsearch1withytsearchN(e.g.,ytsearch3for the top 3 results). - Change audio format: Swap
mp3withm4a,opus, orwavin the--audio-formatflag. - Filter by video duration: Add
--min-duration 10(minimum 10 seconds) or--max-duration 300(max 5 minutes) to skip irrelevant clips. - Save to a specific folder: Modify the
-oflag to include a path, e.g.,./audio_files/%(title)s - ${word}.%(ext)s. - Use youtube-dl instead: Just replace
yt-dlpwithyoutube-dlin the scripts—most parameters are identical, but note that youtube-dl may fail on newer YouTube content.
Troubleshooting Tips
- Words with spaces or special characters are handled automatically thanks to quoted variables in the scripts.
- If downloads fail intermittently, add
--retries 5to the command to retry failed attempts. - Run
yt-dlp --helpto explore all available flags and options for fine-tuning.
内容的提问来源于stack exchange,提问作者professor_png




