-2

So, it's not my code I found it on youtube tutorial(which I can't find now).

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])

It is first downloading ".webm" file(video),then converting to mp3 correctly and after that removing original ".webm" video, I don't want it to remove it, when I run it in CMD it says "Deleting original file .webm (pass -k to keep), but I don't know where to pass -k (I'm not programmer I just copied the code)

Thanks in advance!

1 Answers1

1

you need to pass an additional argument

this is copied from 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

you can look here for how to pass the options

I've tested this syntax and it works for me (Darude Sandstorm)

   download_options = {
    'format': 'bestaudio/best',
    'outtmpl': '%(title)s.%(ext)s',
    'nocheckcertificate': True,
    'keepvideo': True,
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'mp3',
        'preferredquality': '192'
    }]
}
Jpsh
  • 1,697
  • 12
  • 17
  • Doesn't work ` Traceback (most recent call last): File "C:\Users\ngval\Python\downloader.py", line 27, in with youtube_dl.YoutubeDL(download_options) as dl: File "C:\Users\ngval\Python\lib\site-packages\youtube_dl\YoutubeDL.py", line 426, in __init__ pp_class = get_postprocessor(pp_def_raw['key']) File "C:\Users\ngval\Python\lib\site-packages\youtube_dl\postprocessor\__init__.py", line 22, in get_postprocessor return globals()[key + 'PP'] KeyError: 'keep-videoPP' ` This is what I get – nGvalia Sep 12 '20 at 17:10
  • my syntax was wrong, I fixed it and linked the resource (github code) i used to figure it out. – Jpsh Sep 12 '20 at 17:26