0

below is the code for downloading youtube files providing URL, but at present the URL is static, I'm trying to make it dynamic so that user can put URL at runtime. and secondly, I'm also trying to set the path where to save, file by the user but, somehow not able to do

(I'm working on python3 idle with windows)

from __future__ import unicode_literals
import youtube_dl
import urllib
import shutil
ydl_opts = {}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download(['https://youtu.be/zhWDdy_5v2w'])
print("DONE!")
  • For dynamic options you can provide command line options to the script for example, scriptName url_of_file and for the second part of the question, you can take a look here https://stackoverflow.com/questions/41240726/change-the-output-name-when-download-with-youtube-dl-using-python – Hassan Abbas Nov 16 '17 at 10:42
  • didn't get it correctly, I tried to set the path but it just didn't work the way i want – monika razdan Nov 16 '17 at 10:48
  • but that at a static value, i want the URL to be set by user at runtime – monika razdan Nov 16 '17 at 10:51

1 Answers1

0

This is how it should work:

from __future__ import unicode_literals
import sys
import youtube_dl
import urllib
import shutil
ydl_opts = {'outtmpl': '~/Documents/file_name'}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
  ydl.download(sys.argv[1:])
print("DONE!")

Usage: youtube_script.py youtube_url

You can use it like this . Though I have not tested the code but it should work. If i correctly understand, you mean the user can set the url when starting the python script ?, then this is how it should be done or you can take the input in a string and then provide it to the ydl.download function.

Hassan Abbas
  • 1,166
  • 20
  • 47