-
Notifications
You must be signed in to change notification settings - Fork 883
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 多视频格式支持 - 把下载视频也放在 output 下,修改了查找原视频的索引
- Loading branch information
Showing
10 changed files
with
95 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -162,4 +162,5 @@ _model_cache/ | |
*.webm | ||
*.mp3 | ||
.DS_Store | ||
runtime/ | ||
runtime/ | ||
dev/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,34 @@ | ||
import os,sys | ||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | ||
import glob | ||
from yt_dlp import YoutubeDL | ||
|
||
def download_video_ytdlp(url, save_path='./', resolution=1080): | ||
def download_video_ytdlp(url, save_path='output', resolution=1080): | ||
allowed_resolutions = [360, 480, 1080] | ||
if resolution not in allowed_resolutions: | ||
resolution = 1080 | ||
|
||
os.makedirs(save_path, exist_ok=True) | ||
ydl_opts = { | ||
'format': f'bestvideo[height<={resolution}]+bestaudio/best[height<={resolution}]', | ||
'outtmpl': f'{save_path}/%(title)s.%(ext)s' | ||
} | ||
with YoutubeDL(ydl_opts) as ydl: | ||
ydl.download([url]) | ||
|
||
def find_video_files(save_path='output'): | ||
from config import ALLOWED_VIDEO_FORMATS | ||
video_files = [file for file in glob.glob(save_path + "/*") if os.path.splitext(file)[1][1:] in ALLOWED_VIDEO_FORMATS] | ||
video_files = [file for file in video_files if not file.startswith("output/output")] | ||
# if num != 1, raise ValueError | ||
if len(video_files) != 1: | ||
raise ValueError(f"找到的视频数量不唯一,请检查。找到的视频数量: {len(video_files)}") | ||
return video_files[0] | ||
|
||
if __name__ == '__main__': | ||
# 示例用法 | ||
url = input('请输入您想下载的视频URL: ') | ||
resolution = input('请输入所需分辨率 (360/480/1080,默认1080): ') | ||
resolution = int(resolution) if resolution.isdigit() else 1080 | ||
download_video_ytdlp(url, resolution=resolution) | ||
print(f"🎥 视频已下载到 {find_video_files()}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,36 @@ | ||
import streamlit as st | ||
import os, glob, sys, shutil | ||
import os, sys, shutil | ||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | ||
from st_components.imports_and_utils import step1_ytdlp | ||
from core.step1_ytdlp import download_video_ytdlp, find_video_files | ||
|
||
def download_video_section(cloud): | ||
title1 = "上传视频 " if cloud else "下载或上传视频" | ||
st.header(title1) | ||
with st.container(border=True): | ||
if not glob.glob("*.mp4") + glob.glob("*.webm"): | ||
try: | ||
video_file = find_video_files() | ||
st.video(video_file) | ||
if st.button(" 删除并重新选择 ", key="delete_video_button"): | ||
os.remove(video_file) | ||
if os.path.exists("output"): | ||
shutil.rmtree("output") | ||
st.rerun() | ||
return True | ||
except: | ||
if not cloud: | ||
url = st.text_input("输入YouTube链接:") | ||
if st.button("下载视频", key="download_button", use_container_width=True): | ||
if url: | ||
with st.spinner("正在下载视频..."): | ||
step1_ytdlp.download_video_ytdlp(url, save_path='./') | ||
download_video_ytdlp(url) | ||
st.rerun() | ||
|
||
uploaded_file = st.file_uploader("或上传视频 <30min", type=["mp4", "webm"]) | ||
from config import ALLOWED_VIDEO_FORMATS | ||
uploaded_file = st.file_uploader("或上传视频 <30min", type=ALLOWED_VIDEO_FORMATS) | ||
if uploaded_file: | ||
with open(os.path.join("./", uploaded_file.name), "wb") as f: | ||
os.makedirs("output", exist_ok=True) | ||
# 视频写入output文件夹 | ||
with open(os.path.join("output", uploaded_file.name), "wb") as f: | ||
f.write(uploaded_file.getbuffer()) | ||
st.video(uploaded_file) | ||
st.rerun() | ||
else: | ||
return False | ||
else: | ||
video_file = (glob.glob("*.mp4") + glob.glob("*.webm"))[0] | ||
st.video(video_file) | ||
if st.button(" 删除并重新选择 ", key="delete_video_button"): | ||
os.remove(video_file) | ||
if os.path.exists("output"): | ||
shutil.rmtree("output") | ||
st.rerun() | ||
return True | ||
return False |