Lamara091 commited on
Commit
88457d4
·
verified ·
1 Parent(s): f525f6a

Upload yt_dip(個人版).py

Browse files
Files changed (1) hide show
  1. yt_dip(個人版).py +57 -0
yt_dip(個人版).py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """YT-DIP(個人版).ipynb
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1BR2TvWOPERSBX29RxIuc-4oAiRuFy6cM
8
+
9
+ ## YT-DIP 專案說明:
10
+ 本專案採用 "YT-DIP" 庫實現視頻下載與轉換功能,該庫提供了一種高效且可靠的方法來從YouTube下載視頻並轉換成音訊格式。相比傳統的 "youtomp3" 項目的PYtube,本專案的實現方式更為簡潔,欲處理時間得以減少至原項目的1/3。
11
+
12
+ ### 主要功能
13
+ - **音訊轉換**:將下載的視頻檔轉換為高品質的 MP3 格式。
14
+ - **時間剪輯**:支援對下載的視頻進行時間段剪輯,只提取使用者感興趣的部分。
15
+ - **使用者介面**:簡潔明瞭的使用者參數設置介面,支援通過表單直接輸入視頻URL和剪輯時間。
16
+
17
+ ### 開發環境
18
+ - **主要依賴**:YT-DIP, ffmpeg
19
+ - **語言**:Python
20
+ """
21
+
22
+ #@title 必要的庫
23
+ # 安裝 yt-dlp 和 ffmpeg
24
+ !pip install -U yt-dlp
25
+ !apt-get install ffmpeg
26
+
27
+ #@title 設定下載和剪輯參數 { run: "auto", display-mode: "form" }
28
+ #@markdown ### 輸入 影片 URL
29
+ video_url = 'https://www.youtube.com/watch?v=_Em7aKFOJjA' #@param {type:"string"}
30
+ #@markdown ### 剪輯開始時間
31
+ start_time = "00:00:00" #@param {type:"string"}
32
+ #@markdown ### 剪輯結束時間
33
+ end_time = "00:05:30" #@param {type:"string"}
34
+
35
+ # Helper function to convert HH:MM:SS to seconds
36
+ def convert_time_to_seconds(time_str):
37
+ hours, minutes, seconds = map(int, time_str.split(':'))
38
+ return hours * 3600 + minutes * 60 + seconds
39
+
40
+ # 使用帮助函数转换时间
41
+ start_seconds = convert_time_to_seconds(start_time)
42
+ end_seconds = convert_time_to_seconds(end_time)
43
+
44
+ #@title 下載並剪輯視頻
45
+ !yt-dlp -x --audio-format mp3 --audio-quality 128K --ppa "ExtractAudio:-ss {start_seconds} -to {end_seconds}" {video_url} -o "%(title)s.%(ext)s"
46
+ from google.colab import files
47
+ import os
48
+ import glob
49
+ # 使用 glob 模块来更可靠地匹配文件名,尤其是包含特殊字符和空格的情况
50
+ download_files = glob.glob('*.mp3')
51
+ if download_files:
52
+ # 确保正确处理文件名,尤其是包含特殊字符的情况
53
+ download_filename = download_files[0] # 下载第一个找到的 MP3 文件
54
+ print(f"Downloading file: {download_filename}") # 打印将要下载的文件名
55
+ files.download(download_filename)
56
+ else:
57
+ print("No MP3 file found.")