2022年11月3日 星期四

只要點兩下,就能離開位子。讓電腦依照規劃自動下載YT影片


        由於教學上需要,需要從YouTube下載影片。希望能夠做到只要點兩下,就能離開位子。讓電腦依照規劃自動下載YouTube影片。網路上有許多教學如下:
1.Kej's FLV Retriever:
使用Kej's FLV Retriever 將影片網址貼上,按下RETRIEVE NOW! 按鈕後,就會出現下載網址,點選下載網址就可以下載了。但是卻遇到這個畫面:
2.將YouTube影片網址去除ube:
將YouTube影片網址去除ube,就會進到https://yout.com/的網站,選擇所需的格式,就可以下載了,只是下載次數有限制且下載速度慢。但是未來還要收費。
3.使用Youtube Downloader HD程式:
只要將影片網址,按下按鈕,就可以下載。若有一大堆影片需要下載,就要一個一個地下載。不能設定好網址後,批次下載。
        整理一下自己的需求。若自己看到好的教學影片,只要將影片網址記錄在一個TXT檔,只要點兩下,就能依照TXT的規劃,自動地一個一個地下載。換句話說,只要規劃好TXT,點兩下,就可以離開位子,讓電腦自動下載。
下載檔案。解壓密碼:demo1234
使用步驟:

檔案名稱:P001.py
檔案內容:
from pytube import YouTube
from pytube.cli import on_progress
import os

class YouTubeDownloader:
    def __init__(self,URL,FolderPath):
        self.Url = URL
        self.FolderPath = FolderPath
   
    def onProgress(self,stream,chunk,remains):
        total = stream.filesize
        percent = (total-remains)/total*100
        print(f'下載中… {percent:05.2f}%', end='\r')

    def YTDownloader(self):
        yt = YouTube(self.Url,on_progress_callback=self.onProgress)
        print(yt.title+" downloading....")
        yt.streams.filter().get_highest_resolution().download(self.FolderPath)
        print("Download "+yt.title+" OK!")

class OpenFiles():
    #開啟檔案

    def __init__(self,FileName,FolderName):
        self.FileName = FileName
        self.FolderName = FolderName
   
    def FileContentList(self):
        #設定信件內容檔名並取得檔案內容並回傳List
        ContentFileName = str(os.path.abspath(os.getcwd()))+"\\"+self.FileName
        ContentFile = open(ContentFileName,'r')
        FileContentList = ContentFile.readlines()
        ContentFile.close()
        return FileContentList
   
    def GetFolderPath(self):
        #取得資料夾路徑
        if os.path.exists(self.FolderName):
            FolderPath = str(os.path.abspath(os.getcwd()))+"\\"+self.FolderName
        else:
            os.mkdir(self.FolderName)
            FolderPath = str(os.path.abspath(os.getcwd()))+"\\"+self.FolderName

        return FolderPath
   
fcl = OpenFiles('下載名單.txt','').FileContentList()
gfp = OpenFiles('','download').GetFolderPath()
for i in fcl:
    temp=i.replace('\n','')
    YouTubeDownloader(temp,gfp).YTDownloader()

os.system("pause")
os.system("exit")

資料來源:

沒有留言:

張貼留言

Laravel 12 Model 資料庫中的資料表,並提供與資料庫互動的介面

相關系列文章: 1. 在 windows 10 安裝 laravel 12 studentManagement環境與設定 2. laravel 12 route 路由 3. laravel 12 Blade Templates 網頁模版 4. laravel 12 Control...