使用python youtube_dl从youtube下载.mp3 / webm格式的音乐/视频时出现问题

问题描述

所以,这不是我在youtube教程上找到的代码(现在找不到)。

from __future__ import unicode_literals
import youtube_dl
import os
from sys import argv

# Download data and config

download_options = {
    'format': 'bestaudio/best','outtmpl': '%(title)s.%(ext)s','nocheckcertificate': True,'postprocessors': [{
        'key': 'FFmpegExtractAudio','preferredcodec': 'mp3','preferredquality': '192',}],}

# Song Directory
if not os.path.exists('Songs'):
    os.mkdir('Songs')
else:
    os.chdir('Songs')

# Download Songs
with youtube_dl.YoutubedL(download_options) as dl:
    with open('..\\' + argv[1],'r') as f:
        for song_url in f:
            dl.download([song_url])

它首先下载“ .webm”文件(视频),然后正确转换为mp3,然后删除原始的“ .webm”视频,我不希望它删除,当我在CMD中运行它时说“删除原始文件.webm(保留传递-k,但我不知道将-k传递到哪里 (我不是程序员,我只是复制了代码

谢谢!

解决方法

您需要传递一个附加参数

这是从youtube_dl github复制的

--postprocessor-args ARGS        Give these arguments to the postprocessor
-k,--keep-video                 Keep the video file on disk after the post-
                                 processing; the video is erased by default

您可以通过here了解如何传递选项

我已经测试了这种语法,并且对我有用(Darude Sandstorm)

   download_options = {
    'format': 'bestaudio/best','outtmpl': '%(title)s.%(ext)s','nocheckcertificate': True,'keepvideo': True,'postprocessors': [{
        'key': 'FFmpegExtractAudio','preferredcodec': 'mp3','preferredquality': '192'
    }]
}