Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import ffmpeg
|
| 4 |
+
|
| 5 |
+
def create_video_from_images(images_folder, frame_rate):
|
| 6 |
+
# Get list of images
|
| 7 |
+
images = [img for img in os.listdir(images_folder) if img.endswith(".jpg") or img.endswith(".jpeg") or img.endswith(".png")]
|
| 8 |
+
images.sort() # Ensure images are in order
|
| 9 |
+
|
| 10 |
+
if not images:
|
| 11 |
+
return "No images found in the folder."
|
| 12 |
+
|
| 13 |
+
# Create a temporary text file listing all images
|
| 14 |
+
with open("input_images.txt", "w") as file:
|
| 15 |
+
for img in images:
|
| 16 |
+
file.write(f"file '{os.path.join(images_folder, img)}'\n")
|
| 17 |
+
|
| 18 |
+
output_video = os.path.join(images_folder, "output_video.mp4")
|
| 19 |
+
|
| 20 |
+
# Use ffmpeg to create the video
|
| 21 |
+
(
|
| 22 |
+
ffmpeg
|
| 23 |
+
.input("input_images.txt", format='concat', safe=0)
|
| 24 |
+
.output(output_video, r=frame_rate, pix_fmt='yuv420p')
|
| 25 |
+
.run(overwrite_output=True)
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# Remove temporary file
|
| 29 |
+
os.remove("input_images.txt")
|
| 30 |
+
|
| 31 |
+
return output_video
|
| 32 |
+
|
| 33 |
+
def gradio_interface(img_folder, frame_rate):
|
| 34 |
+
return create_video_from_images(img_folder, frame_rate)
|
| 35 |
+
|
| 36 |
+
iface = gr.Interface(
|
| 37 |
+
fn=gradio_interface,
|
| 38 |
+
inputs=[
|
| 39 |
+
gr.inputs.Folder(type="dir", label="Upload Folder with Images"),
|
| 40 |
+
gr.inputs.Slider(minimum=1, maximum=60, default=30, label="Frame Rate (FPS)")
|
| 41 |
+
],
|
| 42 |
+
outputs="file", # Return the path to the created video
|
| 43 |
+
live=True
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
iface.launch()
|