Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import ffmpeg
|
3 |
+
import tempfile
|
4 |
+
import os
|
5 |
+
|
6 |
+
def convert_webm_to_apng_with_fps(input_webm_file, fps):
|
7 |
+
"""
|
8 |
+
アップロードされたWEBMファイルを、指定されたFPSでAPNGに変換する関数
|
9 |
+
"""
|
10 |
+
if input_webm_file is None:
|
11 |
+
raise gr.Error("WEBMファイルをアップロードしてください。")
|
12 |
+
|
13 |
+
input_path = input_webm_file.name
|
14 |
+
|
15 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_output_file:
|
16 |
+
output_path = temp_output_file.name
|
17 |
+
|
18 |
+
print(f"入力ファイル: {input_path}")
|
19 |
+
print(f"出力ファイル: {output_path}")
|
20 |
+
print(f"指定FPS: {fps}")
|
21 |
+
|
22 |
+
try:
|
23 |
+
# ffmpegを使って変換処理を実行
|
24 |
+
# -r {fps}: フレームレートを指定
|
25 |
+
(
|
26 |
+
ffmpeg
|
27 |
+
.input(input_path)
|
28 |
+
.output(output_path, f='apng', r=fps, plays=0) # r=fps を追加
|
29 |
+
.run(overwrite_output=True, capture_stdout=True, capture_stderr=True)
|
30 |
+
)
|
31 |
+
print("変換が正常に完了しました。")
|
32 |
+
return output_path
|
33 |
+
except ffmpeg.Error as e:
|
34 |
+
print("エラー発生: ffmpegの処理に失敗しました。")
|
35 |
+
print("--- stderr ---")
|
36 |
+
print(e.stderr.decode())
|
37 |
+
raise gr.Error(f"変換に失敗しました: {e.stderr.decode()}")
|
38 |
+
finally:
|
39 |
+
pass
|
40 |
+
|
41 |
+
|
42 |
+
with gr.Blocks() as demo_advanced:
|
43 |
+
gr.Markdown(
|
44 |
+
"""
|
45 |
+
# WEBMからAPNGへの変換ツール (FPS調整機能付き)
|
46 |
+
WEBM形式の動画ファイルをアップロードすると、アニメーションPNG(APNG)に変換します。
|
47 |
+
スライダーでフレームレート(FPS)を調整できます。
|
48 |
+
"""
|
49 |
+
)
|
50 |
+
|
51 |
+
with gr.Row():
|
52 |
+
with gr.Column(scale=1):
|
53 |
+
input_video = gr.File(label="WEBMファイルをアップロード", file_types=['.webm'])
|
54 |
+
fps_slider = gr.Slider(
|
55 |
+
minimum=1,
|
56 |
+
maximum=60,
|
57 |
+
value=24, # デフォルト値
|
58 |
+
step=1,
|
59 |
+
label="フレームレート (FPS)",
|
60 |
+
info="値を小さくするとファイルサイズが軽くなりますが、動きがカクカクします。"
|
61 |
+
)
|
62 |
+
convert_button = gr.Button("変換する", variant="primary")
|
63 |
+
with gr.Column(scale=1):
|
64 |
+
output_image = gr.File(label="出力されたAPNGファイル")
|
65 |
+
|
66 |
+
convert_button.click(
|
67 |
+
fn=convert_webm_to_apng_with_fps,
|
68 |
+
inputs=[input_video, fps_slider], # 入力をリストで渡す
|
69 |
+
outputs=output_image
|
70 |
+
)
|
71 |
+
|
72 |
+
|
73 |
+
if __name__ == "__main__":
|
74 |
+
# 基本バージョンか応用バージョンか選んで起動してください
|
75 |
+
# demo.launch()
|
76 |
+
demo_advanced.launch()
|