Upload yt_dip(個人版).py
Browse files- yt_dip(個人版).py +52 -51
yt_dip(個人版).py
CHANGED
@@ -1,57 +1,58 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
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 |
-
|
28 |
-
|
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 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import yt_dlp
|
2 |
+
import gradio as gr
|
3 |
+
import os
|
4 |
+
from glob import glob
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
+
# 安装库的操作在 Hugging Face Spaces 会在 requirements.txt 中处理,这里不再需要
|
7 |
+
# requirements.txt 内容应包括 yt-dlp 和 gradio
|
|
|
|
|
|
|
|
|
|
|
8 |
|
|
|
9 |
def convert_time_to_seconds(time_str):
|
10 |
hours, minutes, seconds = map(int, time_str.split(':'))
|
11 |
return hours * 3600 + minutes * 60 + seconds
|
12 |
|
13 |
+
def download_video(video_url, start_time, end_time):
|
14 |
+
start_seconds = convert_time_to_seconds(start_time)
|
15 |
+
end_seconds = convert_time_to_seconds(end_time)
|
16 |
+
options = {
|
17 |
+
'format': 'bestaudio/best',
|
18 |
+
'postprocessors': [{
|
19 |
+
'key': 'FFmpegExtractAudio',
|
20 |
+
'preferredcodec': 'mp3',
|
21 |
+
'preferredquality': '128',
|
22 |
+
}],
|
23 |
+
'outtmpl': '%(title)s.%(ext)s',
|
24 |
+
'postprocessor_args': [
|
25 |
+
'-ss', str(start_seconds),
|
26 |
+
'-to', str(end_seconds)
|
27 |
+
]
|
28 |
+
}
|
29 |
+
|
30 |
+
with yt_dlp.YoutubeDL(options) as ydl:
|
31 |
+
ydl.download([video_url])
|
32 |
+
|
33 |
+
# 查找和提供下载的文件
|
34 |
+
download_files = glob('*.mp3')
|
35 |
+
if download_files:
|
36 |
+
return f"Download successful: {download_files[0]}", download_files[0]
|
37 |
+
return "No MP3 file found.", None
|
38 |
+
|
39 |
+
def setup_interface():
|
40 |
+
interface = gr.Interface(
|
41 |
+
fn=download_video,
|
42 |
+
inputs=[
|
43 |
+
gr.Textbox(lines=2, placeholder="Enter YouTube video URL here...", label="YouTube Video URL"),
|
44 |
+
gr.Textbox(placeholder="Start Time (e.g., 00:00:00)", label="Start Time (HH:MM:SS)"),
|
45 |
+
gr.Textbox(placeholder="End Time (e.g., 00:05:30)", label="End Time (HH:MM:SS)")
|
46 |
+
],
|
47 |
+
outputs=[
|
48 |
+
gr.Text(label="Status Message"),
|
49 |
+
gr.File(label="Download MP3")
|
50 |
+
],
|
51 |
+
title="YouTube Video Downloader",
|
52 |
+
description="Enter YouTube video URL and specify start and end times to download audio as MP3."
|
53 |
+
)
|
54 |
+
return interface
|
55 |
+
|
56 |
+
if __name__ == "__main__":
|
57 |
+
iface = setup_interface()
|
58 |
+
iface.launch()
|