系列文章:
MP4下載使用步驟教學:
MP3下載使用步驟教學:
感恩員東國小徐千惠資訊組長的建議。
由於學生練舞需要有配樂。除了下載影片MP4,也能下載MP3,希望能簡便使用。換言之,只要貼上所有網址,只要點兩下就能將MP3或MP4下載。在2.只要點兩下,就能離開位子。讓電腦依照公開的PlayList自動下載YT多媒體中,利用公開playlist,來下載MP3或MP4。現在則改寫1.只要點兩下,就能離開位子。讓電腦依照規劃自動下載YT影片的程式碼,讓其可以下載MP3。
程式碼:
檔案名稱: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!")
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
fcl = OpenFiles('下載名單.txt','').FileContentList()
gfp = OpenFiles('','download').GetFolderPath()
TempType = OpenFiles('Mp3orMp4.txt','').FileContentList()
TempType.pop(0)
for i in fcl:
temp=i.replace('\n','')
for k in TempType:
if k.upper() == 'MP3':
YouTubeDownloader(temp,gfp).YTMp3Downloader()
elif k.upper() =='MP4':
YouTubeDownloader(temp,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")
檔案名稱:轉換副檔名為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
資料來源:
感謝世憲組長!
回覆刪除