Spaces:
Sleeping
Sleeping
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() |