0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Python] YouTubeリンクをコピーしたら自動でダウンロードする。音声のみ。

Posted at

必要なライブラリは適宜ダウンロードしてください。

ytdownload_bestaudio.py

# -*- coding: utf-8 -*-

from PIL import ImageGrab, Image
import tkinter
import time as tm1 
import datetime as dt1
import pyperclip 
import os 
from youtube_dl import YoutubeDL

class Application( tkinter.Frame ):
    def __init__(self, master): 
        super().__init__(master) 
        self.flag1 = 0 
        self.path1 = os.path.dirname(__file__) + "\\" 
        self.pack() 
        master.title(u"clipboard capture")
        master.geometry("300x40")
        self.label1 = tkinter.Label(text='')
        self.label1.place(x=10, y=10) 
        self.btn1 = tkinter.Button(master, text='start', command=self.btn_click1, bg='orange')
        self.btn1.place(x=180, y=10, width=100)
        master.after(2000, self.update) 

    def update(self): 
        if self.flag1 == 1: 
            datetime1 = dt1.datetime.now().strftime( "%Y%m%d_%H%M%S" ) 
            file1 = datetime1
            self.label1["text"] = file1 
            ret1 = 0 
            ret1 = self.capture1( file1 ) 
        self.master.after(2000, self.update) 

    def capture1( self, file0 ): 
        ret1 = "0" 
        im1 = ImageGrab.grabclipboard()
        str1 = pyperclip.paste().strip().replace( "\r\n", "\n" )
        pyperclip.copy("")
        if "www.youtube.com" in str1:
            print( str1 ) 
            ytdl( str1 )
            ret1 = "1" 
        elif str1 != "": 
            print( str1 ) 
            file1 = self.path1 + str(file0) + ".txt" 
            write1( file1, str1 ) 
            ret1 = "1" 
        elif isinstance(im1, Image.Image):
            file1 = self.path1 + str(file0) + ".jpg" 
            print( file1 ) 
            im1.save( file1 )
            ret1 = "1" 
        return ret1 

    def btn_click1( self ): 
        self.label1.config( text = '' ) 
        if self.flag1 == 0:
            self.flag1 = 1 
            self.btn1.config(bg = 'red', text = 'stop' ) 
        else: 
            self.flag1 = 0 
            self.btn1.config(bg = 'orange', text = 'start' ) 

def write1( file1, str1 ): 
    with open( file1, 'w', encoding='utf-8' ) as f1: 
        f1.write( str1 ) 
    return 0 

def ytdl( str1 ):
    meta = YoutubeDL().extract_info(str1, download=False)
    title = meta.get('title')
    ext = meta.get('ext')
    ydl_opts = {
        'format': 'bestaudio/best',
        'outtmpl': '%(title)s' + '.%(ext)s'
    }
    with YoutubeDL(ydl_opts) as yt:
        yt.download([str1])
        yt.cache.remove()

def main(): 
    root = tkinter.Tk()
    app = Application( master=root ) 
    app.mainloop()

if __name__ == "__main__": 
    main() 
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?