You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用youtube-dl读取txt列表批量搜索并下载YouTube音频

Solution: Batch Download YouTube Audio for a List of Words

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 (or youtube-dl): Grab it via your package manager (e.g., brew install yt-dlp on macOS, sudo apt install yt-dlp on Debian/Ubuntu) or download directly from its official distribution channels.
  • ffmpeg: Required to extract and convert audio formats. Install via brew 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 ytsearch1 with ytsearchN (e.g., ytsearch3 for the top 3 results).
  • Change audio format: Swap mp3 with m4a, opus, or wav in the --audio-format flag.
  • 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 -o flag to include a path, e.g., ./audio_files/%(title)s - ${word}.%(ext)s.
  • Use youtube-dl instead: Just replace yt-dlp with youtube-dl in 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 5 to the command to retry failed attempts.
  • Run yt-dlp --help to explore all available flags and options for fine-tuning.

内容的提问来源于stack exchange,提问作者professor_png

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:54:04