| import gradio as gr |
| import os |
| import tempfile |
| from moviepy.editor import VideoFileClip, concatenate_videoclips |
|
|
| def merge_videos(video_paths, fps=16): |
| if len(video_paths) < 2: |
| return None, "请至少上传两个视频文件!" |
|
|
| try: |
| |
| clips = [VideoFileClip(path) for path in video_paths] |
|
|
| |
| base_w, base_h = clips[0].size |
| clips = [clip.resize(newsize=(base_w, base_h)) for clip in clips] |
|
|
| |
| final_clip = concatenate_videoclips(clips, method="compose") |
|
|
| |
| out_path = os.path.join(tempfile.gettempdir(), "merged_video.mp4") |
| final_clip.write_videofile(out_path, fps=fps, codec="libx264", audio_codec="aac") |
|
|
| return out_path, "合并成功!" |
| except Exception as e: |
| return None, f"视频合并失败: {str(e)}" |
|
|
| with gr.Blocks() as demo: |
| gr.Markdown("## 🎬 视频合并工具(支持多视频上传)") |
| gr.Markdown("上传两个或以上视频,设置帧率(默认 16),合并后可在线播放和下载。") |
|
|
| with gr.Row(): |
| inp_files = gr.File( |
| label="上传视频(至少两个),可多选 / 拖放", |
| file_types=[".mp4", ".mov", ".avi", ".mkv"], |
| file_count="multiple", |
| type="filepath" |
| ) |
| fps_input = gr.Number(label="合并帧率", value=16, precision=0) |
|
|
| merge_btn = gr.Button("🚀 开始合并") |
|
|
| with gr.Row(): |
| output_video = gr.Video(label="合并后的视频") |
| status = gr.Textbox(label="处理状态", interactive=False) |
|
|
| merge_btn.click(fn=merge_videos, inputs=[inp_files, fps_input], outputs=[output_video, status]) |
|
|
| if __name__ == "__main__": |
| demo.launch(server_name="0.0.0.0", server_port=7860) |
|
|