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

火山引擎 最新活动