標籤

bat (52) 作品 (38) python (21) shell (17) windows (11) 虛擬機 (11) php (10) CPP (6) KMS (6) 程式設計 (6) docker (5) 使用教學 (5) xoops (4) 公文 (4) Apache2 (3) Excel (3) juniper (3) 資料庫 (3) mysql (2) 免動手 (2) 資料結構 (2) 軟體廣播 (2) 電腦維修 (2) Android Studio (1) Apple IPAD管理 (1) Arduino (1) CSS (1) LAMP (1) NAS (1) Ubuntu (1) VHD (1) Windows Server (1) 原因 (1) 程式應用 (1) 程式積木 (1) 編輯器 (1) 雲端硬碟 (1)

2022年11月7日 星期一

只要點兩下,就能離開位子。讓電腦依照公開的PlayList自動下載YT多媒體

        由於在學校分享了只要點兩下,就能離開位子。讓電腦依照規劃自動下載YouTube影片。學校同事希望可以貼上公開的PlayList網址與Mp3 或Mp4 ,接下來就是點兩下,離開座位,讓電腦依照PlayList自動下載YouTube多媒體(Mp3或Mp4)。
        開發過程中,發現下載YouTube多媒體後,儲存在電腦內的檔案會有編碼問題,導致下載中斷。YouTube多媒體的名稱會有簡體字、\ 等問題,導致編碼問題。那就等整個下載完畢後,再利用Python來處理檔案名稱,沒想到還是編碼問題。想了四天,苦思無解。主要原因是無法繞過檔案名稱的編碼問題。
        後來,為了能夠做到[只要點兩下,就讓電腦教室學生機可以解除網站鎖定]。讓電腦教室也能讓教師研習使用,必須解除網站鎖定,免得研習無法進行。進行BAT的開發,沒想到發現了
ren *.mp4 *.mp3 。原因是 * 在BAT 是萬用字元,就可以繞過檔案名稱的編碼問題。

下載檔案。解壓密碼:demo1234

使用步驟教學:

檔案名稱:P002.py
檔案內容:
from pytube import Playlist
from ClassFunc import OpenFiles
from ClassFunc import YouTubeDownloader

gfp = OpenFiles('','download').GetFolderPath()
fcl = OpenFiles('YouTubePlayList.txt','').FileContentList()
TempType = OpenFiles('Mp3orMp4.txt','').FileContentList()
TempType.pop(0)
print(TempType)
for i in fcl:
    TempPlayList = Playlist(i)
    if TempPlayList.video_urls == []:
        print("您的playlist 可能是私有 或是 不公開")
    else:
        for j in TempPlayList.video_urls:
            for k in TempType:
                if k.upper() =='MP3':
                    YouTubeDownloader(j,gfp).YTMp3Downloader()
                elif k.upper() =='MP4':
                    YouTubeDownloader(j,gfp).YTDownloader()

for i in TempType:
    if i.upper() =='MP3':
        print("**************************************************")
        print("您選擇下載mp3檔案,由於編碼的關係,下載時檔案為mp4")
        print("請您於下載完畢後,點選[轉換副檔名為mp3.bat]")
        print("[轉換副檔名為mp3.bat]會完成轉換副檔名由mp4改為mp3")
        print("*************************************************")

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


檔案名稱:ClassFunc.py
檔案內容:
from pytube import YouTube
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+".mp4"+" downloading....")
        yt.streams.filter().get_highest_resolution().download(self.FolderPath)
        print("Download "+".mp4"+yt.title+".mp4"+" OK!")
   
    def YTMp3Downloader(self):
        yt = YouTube(self.Url,on_progress_callback=self.onProgress)
        print(yt.title+".mp4"+" downloading....")
        yt.streams.filter().get_audio_only().download(self.FolderPath)
        print("Download "+yt.title+".mp4"+" 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

檔案名稱:轉換副檔名為mp3.bat
檔案內容:
@echo off 
REM 讀取檔案

for /f "skip=1 delims=" %%a in (Mp3orMp4.txt) do (
    set mp3txt=%%~a
)
echo %mp3txt%
if "%mp3txt%"=="mp3" (
cd  %~dp0\download\
ren *.mp4 *.mp3
)

PAUSE
EXIT


資料來源:
1.Using Playlists
2.下載 Youtube 清單中所有影片
3.只要點兩下,就能離開位子。讓電腦依照規劃自動下載YouTube影片


沒有留言:

張貼留言

只要點兩下,傳統右鍵選單改回Win11右鍵選單

系列文章: 1. 只要點兩下,就能將Win11 右鍵選單 回復 傳統右鍵選單 2. 只要點兩下,傳統右鍵選單改回Win11右鍵選單 上一篇提到只要點兩下,就能將Win11選單回到傳統選單。但是有沒有方法能夠回到Win11選單呢?                    ...