Spaces:
Running
Running
File size: 5,064 Bytes
e975a78 c2e06dd e975a78 |
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
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)
|