Search code examples
pythonpython-2.7python-3.xpyinstalleryoutube-dl

Pyinstaller + driver


i did this script to download music from youtube, and it works in my IDLE and after compilated in .exe still works

from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from tkinter import filedialog
import youtube_dl
import os



class f(object):
    def __init__(self, finestra):
        self.finestra = finestra
        self.top = ttk.Frame(self.finestra, padding="10 10 10 10")
        self.top.grid(column=5, row=5)
        self.top.columnconfigure(1, weight=1)
        self.top.rowconfigure(1, weight=1)

        self.finestra.bind("<Return>",self.esegui_query)

        self.link = StringVar()
        self.esito = StringVar()
        self.bitrate = StringVar()
        self.esito.set("In Attesa")

#RIGA 1
        ttk.Label(self.top, text="Link:").grid(column=0,row=0)
        ttk.Entry(self.top, textvariable=self.link).grid(column=1,row=0)
        ttk.Button(self.top, text="Scarica", command=self.esegui_query).grid(column=2,row=0)
#RIGA 2
        ttk.Label(self.top, text="Bitrate:").grid(column=0,row=1)
        r1 = Radiobutton(self.top, text="192", variable=self.bitrate, value="192", cursor="dot")
        r1.grid(column=1,row=1)
        r2 = Radiobutton(self.top, text="320", variable=self.bitrate, value="320", cursor="dot")
        r2.grid(column=2,row=1)
        r1.select()
        ttk.Label(self.top, textvariable=self.esito).grid(column=3,row=1)

    def esegui_query(self,*argv):
        link = self.link.get()
        bitrate=self.bitrate.get()
        ydl_opts = {
        #'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': bitrate,
                        }],
                    }
        self.esito.set("Downloading...")
        try:
            with youtube_dl.YoutubeDL(ydl_opts) as ydl:
                ydl.download([link])
            self.esito.set("Encoding...")
            for file in (os.listdir('.')):
                if file.endswith('.mp3') or file.endswith('.m4a') or file.endswith('.mp4'):
                    file2 = file[::-1][16:][::-1]
                    break
            self.esito.set("Download Completato")
        except Exception as e:
            self.esito.set("ERRORE")
            messagebox.showwarning("ERRORE",e)



finestra = Tk()
finestra.title("Download Youtube")
f = f(finestra)
finestra.mainloop()

to let him convert songs in mp3, i have downloaded the avprobe driver and placed them in

C:\Program Files\Python 3.5\Scripts

how say the guide.

How can i include those drivers while compiling the script with pyinstaller to let the script works in other pc


Solution

  • You can edit your .spec file to include the driver(s) as extra data:

    a = Analysis(...
     datas=[ ('folder\your_driver', '.') ],
     ...
     )
    

    https://pythonhosted.org/PyInstaller/spec-files.html

    So now that driver will be copied over when you compile with pyinstaller. The issue is that the location of the driver(s) has to be in the same location relative to the .exe so that it will find it on any user's system.

    You can also use the command line argument:

    --resource RESOURCE