|
import gradio as gr |
|
import requests |
|
import os |
|
from ffmpy import FFmpeg |
|
|
|
def extract(url): |
|
|
|
last_slash_index = url.rfind('/') |
|
|
|
return url[:last_slash_index] if last_slash_index != -1 else url |
|
|
|
|
|
|
|
def download_files_until_fail(base_url): |
|
i = 1 |
|
while True: |
|
url = f"{base_url}/{i}.ts" |
|
r = requests.get(url, stream=True) |
|
if r.status_code == 200: |
|
with open(f"video_parts/{i}.ts", 'wb') as f: |
|
for chunk in r.iter_content(1024): |
|
f.write(chunk) |
|
print(f"Downloaded {i}.ts") |
|
i += 1 |
|
else: |
|
print(f"Failed to download {i}.ts, stopping...") |
|
break |
|
return i - 1 |
|
|
|
def download_files_until_fail_t(a): |
|
return int(512) |
|
|
|
|
|
|
|
def concatenate_files(num_files, output_folder, output_filename): |
|
output_path = os.path.join(output_folder, output_filename) |
|
|
|
|
|
os.makedirs(output_folder, exist_ok=True) |
|
|
|
|
|
with open(output_path, 'wb') as outfile: |
|
for i in range(1, num_files + 1): |
|
filepath = f"video_parts/{i}.ts" |
|
if os.path.exists(filepath): |
|
with open(filepath, 'rb') as infile: |
|
outfile.write(infile.read()) |
|
print(f"Added {i}.ts to {output_path}") |
|
|
|
|
|
def refresh(drop): |
|
def get_folders_in_directory_except_specific_ones(directory_path, exclude_folders=('venv', '.idea', 'flagged', 'video_parts')): |
|
|
|
items = os.listdir(directory_path) |
|
|
|
folders = [item for item in items if |
|
os.path.isdir(os.path.join(directory_path, item)) and item not in exclude_folders] |
|
|
|
return tuple(folders) |
|
|
|
|
|
current_directory = os.getcwd() |
|
folders_tuple = get_folders_in_directory_except_specific_ones(current_directory) |
|
return gr.Dropdown(folders_tuple) |
|
|
|
|
|
def srch(name): |
|
return f'{name}/{name}.mp4', f'{name}/{name}.mp4' |
|
|
|
def save(name, link): |
|
if os.path.isdir(name) == True: |
|
return "이미 존재하는 이름입니다.", f'{name}/{name}.mp4' |
|
os.mkdir(name) |
|
f = open(f"{name}/{name}.txt", 'w') |
|
f.write(f'{link}') |
|
f.close() |
|
link = extract(link) |
|
num_files = download_files_until_fail(link) |
|
concatenate_files(num_files, name, f"{name}.ts") |
|
input_ts = f"{name}/{name}.ts" |
|
output_mp4 = f'{name}/{name}.mp4' |
|
ff = FFmpeg( |
|
inputs={input_ts: None}, |
|
outputs={output_mp4: '-c:v libx265 -crf 28'} |
|
) |
|
|
|
ff.run() |
|
|
|
|
|
|
|
|
|
|
|
return "saved", f'{name}/{name}.mp4' |
|
|
|
|
|
with gr.Blocks() as demo: |
|
with gr.Tab("Save"): |
|
name = gr.Textbox(label="저장할 이름") |
|
link = gr.Textbox(label="https://---/숫자.ts 그대로 입력") |
|
submit = gr.Button("제출") |
|
out = gr.Textbox() |
|
file = gr.File(interactive=False) |
|
submit.click(save, [name, link], [out, file]) |
|
with gr.Tab("Folder") as folder: |
|
drop = gr.Dropdown([]) |
|
file = gr.File(interactive=False) |
|
vid = gr.Video(interactive=False) |
|
drop.change(srch, [drop], [file, vid]) |
|
folder.select(refresh, [drop], [drop]) |
|
|
|
demo.launch(server_name="0.0.0.0") |
|
|