Pyinstaller + 驱动程序

Pyinstaller + driver

我做了这个脚本来从 youtube 下载音乐,它在我的 IDLE 中工作,在 .exe 中编译后仍然工作

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

为了让他把歌曲转成mp3,我下载了avprobe驱动放在

C:\Program Files\Python 3.5\Scripts

导游怎么说。

如何在使用 pyinstaller 编译脚本时包含这些驱动程序以使脚本在其他 pc 上运行

您可以编辑 .spec 文件以将驱动程序作为额外数据包含在内:

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

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

所以现在当你用pyinstaller编译时驱动会被复制过来。问题是驱动程序的位置必须与 .exe 位于同一位置,这样它才能在任何用户的系统上找到它。

您也可以使用命令行参数:

--resource RESOURCE