0

I am attempting to paste the youtube video URL into the tkinter GUI text box, and have the 'Browse' button initiate a subprocess call that takes a hard-coded youtube-dl code, and adds the text box URL. I can't seem to pass the hard-coded Youtube-dl code, and variable to command line.

Googled multiple things to find answers but can't seem to find any...

from tkinter import *
import tkinter as tk
from tkinter import ttk
import os
import subprocess


root = Tk()
root.geometry("900x800")    
Title = root.title( "JJ's Youtube Downloader")
PathTextBox = Text(root,height =  2)
PathTextBox.grid(row = 4,column = 1,columnspan = 2)
path = PathTextBox.get('1.0',END)
def download():
    import subprocess
    subprocess.call(["youtube-dl -x --audio-format mp3 ", path])


BrowseButton = Button(root,text="Browse ",command = download)
BrowseButton.grid(row = 2,column = 2)

root.configure(background='ivory2')
HeadLabel1 = Label(root,text="Image ")
HeadLabel1.grid(row = 1,column = 1,sticky=(E))
HeadLabel2 = Label(root,text="Reader ")
HeadLabel2.grid(row = 1,column = 2,sticky=(W))
InputLabel = Label(root,text = "INPUT IMAGE")
InputLabel.grid(row = 2, column = 1)


PathLabel = Label(root,text = "Path:")
PathLabel.grid(row = 3,column = 1,sticky=(W))



DataLabel = Label(root,text = "DATA IN IMAGE:")
DataLabel.grid(row = 6,column = 1,sticky=(W+E+N+S))
ResultTextBox = Text(root,height = 30,padx = 1, pady = 1)
ResultTextBox.grid(row = 7,column = 1,columnspan = 4,padx=3, 
pady=3,sticky=W+E+N+S)

root.mainloop()

ERROR CODE I'M RECEIVING Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\hutch\AppData\Local\Programs\Python\Python37\lib\tkinter__init__.py", line 1705, in call return self.func(*args) File "C:\Users\hutch\Downloads\Desktop\PythonPrograms\YoutubeDownloader.py", line 17, in download subprocess.call(["youtube-dl -x --audio-format mp3 ", path]) File "C:\Users\hutch\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 323, in call with Popen(*popenargs, **kwargs) as p: File "C:\Users\hutch\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 775, in init restore_signals, start_new_session) File "C:\Users\hutch\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 1178, in _execute_child startupinfo) FileNotFoundError: [WinError 2] The system cannot find the file specified

2
  • Sorry about the error code not pasting well... Commented Jun 5, 2019 at 22:32
  • 2
    Try subprocess.call(["youtube-dl", "-x", "--audio-format", "mp3", path]). Or, as youtube-dl is a python program, you can just import it. Commented Jun 5, 2019 at 22:36

1 Answer 1

0

Instead of needing to call youtube-dl as a subprocess, you can import youtube-dl itself direct from PyPI and use it like you would any other Python library.

You can add it to your Python environment by running pip install youtube_dl in your terminal/command prompt, and then call YoutubeDL.download() from within your script. To use the command-line arguments you've included in your subprocess string, you can try the following (This example is slightly modified from the documentation, it will download the audio track from your video, then convert to MP3, doing pretty much the same thing as defining -x in your command line arguments)

import youtube_dl

ydl_opts = {
    'format': 'bestaudio/best',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'mp3',
        'preferredquality': '192',
    }]
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download([path])
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Hoppeduppeanut ! The code runs, but two command windows pop up for a millisecond and then disappear... The song doesn't download to the usual folder... I can't see what the command windows say because they disappear too quickly.
I'll need to do some investigation once I'm able to use an interpreter again, but I'd say check the path where you're running your script and see if it's ended up in there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.