innoai's picture
Update app.py
c2e06dd verified
import os
import zipfile
import io
import tempfile
import requests
from moviepy.editor import ImageSequenceClip, concatenate_videoclips, ImageClip
import gradio as gr
from gradio_client import Client, handle_file
def download_file(file_url):
response = requests.get(file_url)
response.raise_for_status() # 确保请求成功
return response.content
def get_images_from_api(uploaded_file):
client = Client("https://pptx2jpgapi.webgpu.click/")
result = client.predict(
uploaded_file=handle_file(uploaded_file.name), # 使用 handle_file 处理上传的文件
mode="Convert each slide to a separate JPG", # 固定模式
api_name="/convert_pptx_to_jpg"
)
print(result)
print(result[0]) #
# file_url = f"https://pptx2jpgapi.webgpu.click/file={result[0]}"
# print(file_url)
# # 下载ZIP文件内容
# zip_content = download_file(file_url)
# 创建临时目录
temp_dir = tempfile.mkdtemp()
# 解压返回的zip文件
# with zipfile.ZipFile(io.BytesIO(zip_content), 'r') as zip_ref:
# zip_ref.extractall(temp_dir)
with zipfile.ZipFile(result[0], 'r') as zip_ref:
zip_ref.extractall(temp_dir)
return temp_dir
def apply_transition(clip1, clip2, transition_type, transition_time):
if transition_type == "crossfade":
return clip1.crossfadeout(transition_time), clip2.crossfadein(transition_time)
elif transition_type == "fadeout":
return clip1.fadeout(transition_time), clip2
elif transition_type == "fadein":
return clip1, clip2.fadein(transition_time)
elif transition_type == "none":
return clip1, clip2
else:
raise ValueError(f"Unsupported transition type: {transition_type}")
def images_to_video(image_folder, fps=24, display_time=3, transition_time=1, transition_type="none"):
# 获取所有jpg文件并按名称排序
image_files = sorted([os.path.join(image_folder, img) for img in os.listdir(image_folder) if img.endswith(".jpg")])
if not image_files:
raise ValueError("No jpg files found in the directory.")
clips = []
for img_path in image_files:
img_clip = ImageClip(img_path, duration=display_time)
# 调整图片尺寸,保持比例,最短边为720
w, h = img_clip.size
if w < h:
img_clip = img_clip.resize(width=720)
else:
img_clip = img_clip.resize(height=720)
clips.append(img_clip)
# 添加过渡效果
final_clips = []
for i in range(len(clips) - 1):
clip1, clip2 = apply_transition(clips[i], clips[i+1], transition_type, transition_time)
final_clips.append(clip1)
final_clips.append(clip2)
final_clips.append(clips[-1])
video = concatenate_videoclips(final_clips, method="compose")
video_path = os.path.join(tempfile.gettempdir(), "output_video.mp4")
video.write_videofile(video_path, fps=fps)
return video_path
def generate_video(uploaded_file, display_time, transition_type, transition_time):
image_folder = get_images_from_api(uploaded_file)
video_path = images_to_video(image_folder, display_time=display_time, transition_time=transition_time, transition_type=transition_type)
return video_path
# Gradio interface
with gr.Blocks(analytics_enabled=False,title="PPTX to Video Converter") as demo:
gr.Markdown("""
# PPTX to Video Converter
## Convert Your PPTX Files to Videos Easily
This tool allows you to convert PPTX files to video format. You can set the display time for each slide, choose different transition effects, and generate a complete video with transitions.
### How to Use
1. Upload your PPTX file.
2. Set the display time for each slide.
3. Choose a transition effect type.
4. Set the transition time.
5. Click the "Generate Video" button, and wait for the video to be generated and displayed on the right.
""")
with gr.Row():
with gr.Column():
uploaded_file = gr.File(label="Upload PPTX File", file_types=[".pptx"])
display_time = gr.Slider(label="Image Display Time (seconds)", minimum=1, maximum=5, step=1, value=3, info="Set the display time for each slide")
transition_type = gr.Radio(["crossfade", "fadeout", "fadein", "none"], label="Transition Type", value="none", info="Choose the type of transition effect")
transition_time = gr.Slider(label="Transition Time (seconds)", minimum=1, maximum=2, step=1, value=1, info="Set the transition time")
generate_button = gr.Button("Generate Video")
with gr.Column():
video_output = gr.Video(label="Generated Video")
generate_button.click(
fn=generate_video,
inputs=[uploaded_file, display_time, transition_type, transition_time],
outputs=video_output
)
# 运行Gradio应用
if __name__ == "__main__":
demo.queue(default_concurrency_limit=5,max_size=20)
demo.launch(show_error=True)