본문 바로가기
지식/Python

pytube 로 mp3/mp4 download

by TheEC 2022. 5. 29.

pytube는 dependency가 없는 youtube 비디오를 다운로드할 수 있는 라이브러리다.

pytube — pytube 12.1.0 documentation

 

pytube — pytube 12.1.0 documentation

© Copyright Revision 2e307d8d.

pytube.io

 

from tkinter import *
from tkinter import messagebox
from pytube import YouTube
import glob
import os.path
import subprocess

# set
root = Tk()
root.title("my converter")
root.geometry("500x200")
root.resizable(False, False)

DOWNLOAD_FOLDER = "D:\\youtube\\"

#convert
def convert():
    #유튜브 전용 인스턴스 생성
    par = lnk.get()
    print(par)

    yt = YouTube(par)
    print("start!")
    if(Radiovar.get() == 1):
        print("type: mp4")
        yt.streams.filter(file_extension='mp4')
        yt.streams.get_highest_resolution().download(DOWNLOAD_FOLDER)
        #yt.streams.filter().first().download(DOWNLOAD_FOLDER) 
    else:
        print("type: mp3")
        yt.streams.filter(only_audio=True).all()
        yt.streams.filter(only_audio=True).first().download(DOWNLOAD_FOLDER) 
        print("success")

        src = yt.streams.first().default_filename[:-4] + 'mp4'
        print(src)
        print(DOWNLOAD_FOLDER, src[:-3]+'mp3')

        # mp4에서 mp3로 변환
        subprocess.call(['ffmpeg', '-i', os.path.join(DOWNLOAD_FOLDER, src), os.path.join(DOWNLOAD_FOLDER, src[0:-3]+'mp3')])

        # 변환되기 전 mp4파일 삭제 
        os.remove(os.path.join(DOWNLOAD_FOLDER, src))

    messagebox.showinfo("success","converted!") #메시지 박스를 띄운다.


#main
lbl = Label(root, text="YouTube Converter!")
lbl.pack()

lnk = Entry(root)
lnk.pack(fill="x")

st = StringVar()
Radiovar = IntVar()
Radio_button1 = Radiobutton(text="mp4",variable=Radiovar,value=1) 
Radio_button2 = Radiobutton(text="mp3(default)",variable=Radiovar,value=2)
Radio_button1.pack()
Radio_button2.pack()

place = Label(root, text="\n")
place.pack()

btn = Button(root, text="convert",command=convert)
btn.pack()
root.mainloop()

 

 

Ignoring exception in on_message
Traceback (most recent call last):
File "/.local/lib/python3.9/site-packages/pytube/__main__.py", line 177, in fmt_streams extract.apply_signature(stream_manifest, self.vid_info, self.js)
File "/.local/lib/python3.9/site-packages/pytube/extract.py", line 409, in apply_signature cipher = Cipher(js=js)
File "/.local/lib/python3.9/site-packages/pytube/cipher.py", line 33, in __init__
raise RegexMatchError(pytube.exceptions.RegexMatchError: __init__: could not find match for ^\\w+\\W
cipher.py

var_regex = re.compile(r"^\w+\W")
=> var_regex = re.compile(r"^\$*\w+\W")

 

댓글