raithing commited on
Commit
a4d6f6f
·
verified ·
1 Parent(s): 74998e6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -0
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
20
+ # 処理したファイルの数を追跡
21
+ processed_count = 0
22
+
23
+ # 全てのmp4ファイルを処理
24
+ for root, dirs, files in os.walk(temp_dir):
25
+ for file in files:
26
+ if file.lower().endswith('.mp4'):
27
+ input_path = os.path.join(root, file)
28
+
29
+ # 出力パスの作成(元のディレクトリ構造を維持)
30
+ relative_path = os.path.relpath(os.path.join(root, file), temp_dir)
31
+ output_path = os.path.join(output_temp_dir, relative_path)
32
+
33
+ # 出力ディレクトリの作成
34
+ os.makedirs(os.path.dirname(output_path), exist_ok=True)
35
+
36
+ # ビデオのリサイズ処理
37
+ cap = cv2.VideoCapture(input_path)
38
+
39
+ # 入力ビデオのプロパティを取得
40
+ fps = int(cap.get(cv2.CAP_PROP_FPS))
41
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
42
+
43
+ # 出力ビデオライターの設定
44
+ out = cv2.VideoWriter(output_path, fourcc, fps, (1080, 1920))
45
+
46
+ while cap.isOpened():
47
+ ret, frame = cap.read()
48
+ if not ret:
49
+ break
50
+
51
+ # フレームをリサイズ
52
+ resized_frame = cv2.resize(frame, (1080, 1920))
53
+ out.write(resized_frame)
54
+
55
+ cap.release()
56
+ out.release()
57
+ processed_count += 1
58
+
59
+ if processed_count == 0:
60
+ return None
61
+
62
+ # 処理済みファイルをZIPにまとめる
63
+ output_zip = tempfile.NamedTemporaryFile(delete=False, suffix='.zip')
64
+ with zipfile.ZipFile(output_zip.name, 'w', zipfile.ZIP_DEFLATED) as zf:
65
+ for root, dirs, files in os.walk(output_temp_dir):
66
+ for file in files:
67
+ file_path = os.path.join(root, file)
68
+ arcname = os.path.relpath(file_path, output_temp_dir)
69
+ zf.write(file_path, arcname)
70
+
71
+ # 一時ディレクトリの削除
72
+ shutil.rmtree(output_temp_dir)
73
+
74
+ return output_zip.name
75
+
76
+ # Gradioインターフェースの作成
77
+ with gr.Blocks() as app:
78
+ gr.Markdown("## 動画リサイズツール")
79
+ gr.Markdown("1. フォルダ内の動画ファイルをZIPにまとめてアップロードしてください")
80
+ gr.Markdown("2. 全ての動画が768x1280から1080x1920にリサイズされます")
81
+
82
+ with gr.Row():
83
+ input_file = gr.File(label="ZIPファイルをアップロード", file_types=[".zip"])
84
+ output_file = gr.File(label="処理済みZIPファイル")
85
+
86
+ upload_button = gr.Button("処理開始")
87
+ upload_button.click(fn=process_videos, inputs=[input_file], outputs=[output_file])
88
+
89
+ gr.Markdown("### 注意事項")
90
+ gr.Markdown("- アップロードするZIPファイルには.mp4ファイルが含まれている必要があります")
91
+ gr.Markdown("- 元のフォルダ構造は維持されます")
92
+ gr.Markdown("- 処理には時間がかかる場合があります")
93
+
94
+ if __name__ == "__main__":
95
+ app.launch()