File size: 3,997 Bytes
a4d6f6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import gradio as gr
import os
import zipfile
import tempfile
import shutil
from pathlib import Path
import cv2
from tqdm import tqdm

def process_videos(zip_file):
    # 一時ディレクトリを作成
    with tempfile.TemporaryDirectory() as temp_dir:
        # ZIPファイルを一時ディレクトリに解凍
        with zipfile.ZipFile(zip_file.name, 'r') as zip_ref:
            zip_ref.extractall(temp_dir)
        
        # 出力用の一時ディレクトリを作成
        output_temp_dir = tempfile.mkdtemp()
        
        # 処理したファイルの数を追跡
        processed_count = 0
        
        # 全てのmp4ファイルを処理
        for root, dirs, files in os.walk(temp_dir):
            for file in files:
                if file.lower().endswith('.mp4'):
                    input_path = os.path.join(root, file)
                    
                    # 出力パスの作成(元のディレクトリ構造を維持)
                    relative_path = os.path.relpath(os.path.join(root, file), temp_dir)
                    output_path = os.path.join(output_temp_dir, relative_path)
                    
                    # 出力ディレクトリの作成
                    os.makedirs(os.path.dirname(output_path), exist_ok=True)
                    
                    # ビデオのリサイズ処理
                    cap = cv2.VideoCapture(input_path)
                    
                    # 入力ビデオのプロパティを取得
                    fps = int(cap.get(cv2.CAP_PROP_FPS))
                    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
                    
                    # 出力ビデオライターの設定
                    out = cv2.VideoWriter(output_path, fourcc, fps, (1080, 1920))
                    
                    while cap.isOpened():
                        ret, frame = cap.read()
                        if not ret:
                            break
                        
                        # フレームをリサイズ
                        resized_frame = cv2.resize(frame, (1080, 1920))
                        out.write(resized_frame)
                    
                    cap.release()
                    out.release()
                    processed_count += 1
        
        if processed_count == 0:
            return None
        
        # 処理済みファイルをZIPにまとめる
        output_zip = tempfile.NamedTemporaryFile(delete=False, suffix='.zip')
        with zipfile.ZipFile(output_zip.name, 'w', zipfile.ZIP_DEFLATED) as zf:
            for root, dirs, files in os.walk(output_temp_dir):
                for file in files:
                    file_path = os.path.join(root, file)
                    arcname = os.path.relpath(file_path, output_temp_dir)
                    zf.write(file_path, arcname)
        
        # 一時ディレクトリの削除
        shutil.rmtree(output_temp_dir)
        
        return output_zip.name

# Gradioインターフェースの作成
with gr.Blocks() as app:
    gr.Markdown("## 動画リサイズツール")
    gr.Markdown("1. フォルダ内の動画ファイルをZIPにまとめてアップロードしてください")
    gr.Markdown("2. 全ての動画が768x1280から1080x1920にリサイズされます")
    
    with gr.Row():
        input_file = gr.File(label="ZIPファイルをアップロード", file_types=[".zip"])
        output_file = gr.File(label="処理済みZIPファイル")
    
    upload_button = gr.Button("処理開始")
    upload_button.click(fn=process_videos, inputs=[input_file], outputs=[output_file])
    
    gr.Markdown("### 注意事項")
    gr.Markdown("- アップロードするZIPファイルには.mp4ファイルが含まれている必要があります")
    gr.Markdown("- 元のフォルダ構造は維持されます")
    gr.Markdown("- 処理には時間がかかる場合があります")

if __name__ == "__main__":
    app.launch()