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

如何修改youtube-dl程序下载生成的.wav文件名为audio1.wav?

How to Rename youtube-dl's Output WAV File to audio1.wav

Got it, here are a few reliable ways to get your converted audio file renamed to audio1.wav instead of that long auto-generated name:

Method 1: Set Output Filename Directly in youtube-dl Config (Simplest)

You can add the outtmpl parameter to your ydl_opts to define a custom filename template. This handles both the downloaded video (since you have keepvideo=True) and the converted audio in one step.

Here's the modified code:

from __future__ import unicode_literals
import youtube_dl

ydl_opts = {
    'format': 'bestaudio/best',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'wav',
        'preferredquality': '192'
    }],
    'postprocessor_args': [
        '-ar', '16000'
    ],
    'prefer_ffmpeg': True,
    'keepvideo': True,
    # Define custom output filename template
    'outtmpl': 'audio1.%(ext)s'
}

with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download(['https://www.youtube.com/watch?v=JpjEwIceVIo'])

This will save the video as audio1.webm and the converted audio as audio1.wav—no extra steps needed.

Method 2: Rename After Download (Keep Original Video Filename)

If you want to keep the original video filename but still rename the audio, use Python's os module to rename the file after processing. Using extract_info() instead of download() lets you get the exact filename from youtube-dl's metadata, making this more reliable than globbing for files.

Check out this version:

from __future__ import unicode_literals
import youtube_dl
import os

ydl_opts = {
    'format': 'bestaudio/best',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'wav',
        'preferredquality': '192'
    }],
    'postprocessor_args': [
        '-ar', '16000'
    ],
    'prefer_ffmpeg': True,
    'keepvideo': True
}

with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    # Extract info and download the file
    info_dict = ydl.extract_info('https://www.youtube.com/watch?v=JpjEwIceVIo', download=True)
    # Get the original filename (without extension)
    original_base = ydl.prepare_filename(info_dict).rsplit('.', 1)[0]
    # Construct the path to the converted WAV file
    original_wav = f"{original_base}.wav"
    # Rename to audio1.wav
    os.rename(original_wav, 'audio1.wav')
    print(f"Successfully renamed: {original_wav} → audio1.wav")

This keeps your video file with its original long name, while renaming only the audio to audio1.wav.

Method 3: Quick Glob-Based Rename (For Simple Use Cases)

If you're running this script in a directory where only this WAV file is present, you can use glob to find and rename it quickly:

from __future__ import unicode_literals
import youtube_dl
import os
import glob

ydl_opts = {
    'format': 'bestaudio/best',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'wav',
        'preferredquality': '192'
    }],
    'postprocessor_args': [
        '-ar', '16000'
    ],
    'prefer_ffmpeg': True,
    'keepvideo': True
}

with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download(['https://www.youtube.com/watch?v=JpjEwIceVIo'])

# Find all WAV files in the current directory
wav_files = glob.glob('*.wav')
if wav_files:
    os.rename(wav_files[0], 'audio1.wav')
    print(f"Renamed {wav_files[0]} to audio1.wav!")
else:
    print("No WAV file found to rename.")

Note: This works best if you don't have other WAV files in the same folder, otherwise it might rename the wrong file.


内容的提问来源于stack exchange,提问作者Y V Sravan Kumar Reddy

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 14:42:31