Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import zipfile
|
4 |
+
import tempfile
|
5 |
+
import shutil
|
6 |
+
from pathlib import Path
|
7 |
+
import cv2
|
8 |
+
from tqdm import tqdm
|
9 |
+
|
10 |
+
def process_videos(zip_file):
|
11 |
+
# 一時ディレクトリを作成
|
12 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
13 |
+
# ZIPファイルを一時ディレクトリに解凍
|
14 |
+
with zipfile.ZipFile(zip_file.name, 'r') as zip_ref:
|
15 |
+
zip_ref.extractall(temp_dir)
|
16 |
+
|
17 |
+
# 出力用の一時ディレクトリを作成
|
18 |
+
output_temp_dir = tempfile.mkdtemp()
|
19 |
+
output_upscaled_dir = tempfile.mkdtemp()
|
20 |
+
|
21 |
+
# 処理したファイルの数を追跡
|
22 |
+
processed_count = 0
|
23 |
+
|
24 |
+
# 全てのmp4ファイルを処理
|
25 |
+
for root, dirs, files in os.walk(temp_dir):
|
26 |
+
for file in files:
|
27 |
+
if file.lower().endswith('.mp4'):
|
28 |
+
input_path = os.path.join(root, file)
|
29 |
+
|
30 |
+
# 出力パスの作成(元のディレクトリ構造を維持)
|
31 |
+
relative_path = os.path.relpath(os.path.join(root, file), temp_dir)
|
32 |
+
output_path = os.path.join(output_temp_dir, relative_path)
|
33 |
+
output_upscaled_path = os.path.join(output_upscaled_dir, f"upscaled_{relative_path}")
|
34 |
+
|
35 |
+
# 出力ディレクトリの作成
|
36 |
+
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
37 |
+
os.makedirs(os.path.dirname(output_upscaled_path), exist_ok=True)
|
38 |
+
|
39 |
+
# ビデオの読み込み
|
40 |
+
cap = cv2.VideoCapture(input_path)
|
41 |
+
|
42 |
+
# ビデオのプロパティを取得
|
43 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
44 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
45 |
+
|
46 |
+
# 出力ビデオライターの設定
|
47 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
48 |
+
out_normal = cv2.VideoWriter(output_path, fourcc, fps, (1080, 1920))
|
49 |
+
out_upscaled = cv2.VideoWriter(output_upscaled_path, fourcc, fps, (2160, 3840))
|
50 |
+
|
51 |
+
# プログレスバー用
|
52 |
+
pbar = tqdm(total=total_frames, desc=f"Processing {file}")
|
53 |
+
|
54 |
+
while cap.isOpened():
|
55 |
+
ret, frame = cap.read()
|
56 |
+
if not ret:
|
57 |
+
break
|
58 |
+
|
59 |
+
# 通常のリサイズ (1080x1920)
|
60 |
+
resized_frame = cv2.resize(frame, (1080, 1920))
|
61 |
+
out_normal.write(resized_frame)
|
62 |
+
|
63 |
+
# 高解像度アップスケール (2160x3840)
|
64 |
+
# INTER_CUBIC を使用してより良い品質を得る
|
65 |
+
upscaled_frame = cv2.resize(resized_frame, (2160, 3840),
|
66 |
+
interpolation=cv2.INTER_CUBIC)
|
67 |
+
out_upscaled.write(upscaled_frame)
|
68 |
+
|
69 |
+
pbar.update(1)
|
70 |
+
|
71 |
+
pbar.close()
|
72 |
+
cap.release()
|
73 |
+
out_normal.release()
|
74 |
+
out_upscaled.release()
|
75 |
+
processed_count += 1
|
76 |
+
|
77 |
+
if processed_count == 0:
|
78 |
+
return None
|
79 |
+
|
80 |
+
# 処理済みファイルをZIPにまとめる
|
81 |
+
output_zip = tempfile.NamedTemporaryFile(delete=False, suffix='.zip')
|
82 |
+
with zipfile.ZipFile(output_zip.name, 'w', zipfile.ZIP_DEFLATED) as zf:
|
83 |
+
# 通常サイズの動画を追加
|
84 |
+
for root, dirs, files in os.walk(output_temp_dir):
|
85 |
+
for file in files:
|
86 |
+
file_path = os.path.join(root, file)
|
87 |
+
arcname = os.path.relpath(file_path, output_temp_dir)
|
88 |
+
zf.write(file_path, arcname)
|
89 |
+
|
90 |
+
# アップスケールされた動画を追加
|
91 |
+
for root, dirs, files in os.walk(output_upscaled_dir):
|
92 |
+
for file in files:
|
93 |
+
file_path = os.path.join(root, file)
|
94 |
+
arcname = os.path.relpath(file_path, output_upscaled_dir)
|
95 |
+
zf.write(file_path, f"upscaled/{arcname}")
|
96 |
+
|
97 |
+
# 一時ディレクトリの削除
|
98 |
+
shutil.rmtree(output_temp_dir)
|
99 |
+
shutil.rmtree(output_upscaled_dir)
|
100 |
+
|
101 |
+
return output_zip.name
|
102 |
+
|
103 |
+
# Gradioインターフェースの作成
|
104 |
+
with gr.Blocks() as app:
|
105 |
+
gr.Markdown("## ビデオアップスケーラー")
|
106 |
+
gr.Markdown("1. フォルダ内の動画ファイルをZIPにまとめてアップロードしてください")
|
107 |
+
gr.Markdown("2. 全ての動画が以下の2つの解像度で出力されます:")
|
108 |
+
gr.Markdown(" - 標準解像度: 1080x1920")
|
109 |
+
gr.Markdown(" - 高解像度: 2160x3840")
|
110 |
+
|
111 |
+
with gr.Row():
|
112 |
+
input_file = gr.File(label="ZIPファイルをアップロード", file_types=[".zip"])
|
113 |
+
output_file = gr.File(label="処理済みZIPファイル")
|
114 |
+
|
115 |
+
upload_button = gr.Button("処理開始")
|
116 |
+
upload_button.click(fn=process_videos, inputs=[input_file], outputs=[output_file])
|
117 |
+
|
118 |
+
gr.Markdown("### 注意事項")
|
119 |
+
gr.Markdown("- アップロードするZIPファイルには.mp4ファイルが含まれている必要があります")
|
120 |
+
gr.Markdown("- 元のフォルダ構造は維持されます")
|
121 |
+
gr.Markdown("- 処理には時間がかかる場合があります")
|
122 |
+
|
123 |
+
if __name__ == "__main__":
|
124 |
+
app.launch()
|