File size: 2,743 Bytes
4a08892
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import ffmpeg
import tempfile
import os

def convert_webm_to_apng_with_fps(input_webm_file, fps):
    """
    アップロードされたWEBMファイルを、指定されたFPSでAPNGに変換する関数
    """
    if input_webm_file is None:
        raise gr.Error("WEBMファイルをアップロードしてください。")

    input_path = input_webm_file.name

    with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_output_file:
        output_path = temp_output_file.name

    print(f"入力ファイル: {input_path}")
    print(f"出力ファイル: {output_path}")
    print(f"指定FPS: {fps}")

    try:
        # ffmpegを使って変換処理を実行
        # -r {fps}: フレームレートを指定
        (
            ffmpeg
            .input(input_path)
            .output(output_path, f='apng', r=fps, plays=0) # r=fps を追加
            .run(overwrite_output=True, capture_stdout=True, capture_stderr=True)
        )
        print("変換が正常に完了しました。")
        return output_path
    except ffmpeg.Error as e:
        print("エラー発生: ffmpegの処理に失敗しました。")
        print("--- stderr ---")
        print(e.stderr.decode())
        raise gr.Error(f"変換に失敗しました: {e.stderr.decode()}")
    finally:
        pass


with gr.Blocks() as demo_advanced:
    gr.Markdown(
        """
        # WEBMからAPNGへの変換ツール (FPS調整機能付き)
        WEBM形式の動画ファイルをアップロードすると、アニメーションPNG(APNG)に変換します。
        スライダーでフレームレート(FPS)を調整できます。
        """
    )

    with gr.Row():
        with gr.Column(scale=1):
            input_video = gr.File(label="WEBMファイルをアップロード", file_types=['.webm'])
            fps_slider = gr.Slider(
                minimum=1,
                maximum=60,
                value=24, # デフォルト値
                step=1,
                label="フレームレート (FPS)",
                info="値を小さくするとファイルサイズが軽くなりますが、動きがカクカクします。"
            )
            convert_button = gr.Button("変換する", variant="primary")
        with gr.Column(scale=1):
            output_image = gr.File(label="出力されたAPNGファイル")

    convert_button.click(
        fn=convert_webm_to_apng_with_fps,
        inputs=[input_video, fps_slider], # 入力をリストで渡す
        outputs=output_image
    )


if __name__ == "__main__":
    # 基本バージョンか応用バージョンか選んで起動してください
    # demo.launch()
    demo_advanced.launch()