feat: images to video

This commit is contained in:
Artemy 2023-04-16 09:55:33 +03:00
parent ca3d0969bc
commit 7bf970cf1b
5 changed files with 57 additions and 21 deletions

2
app.py
View file

@ -23,7 +23,7 @@ for tab_name in tabs:
all_tabs.append((tab_name, tab.app)) all_tabs.append((tab_name, tab.app))
except Exception as e: except Exception as e:
print(f"Error loading tab: {e}") print(f"Error loading tab ({tab_name}): {e}")
with gr.Blocks() as app: with gr.Blocks() as app:
for tab_name, tab in all_tabs: for tab_name, tab in all_tabs:

View file

@ -1,11 +1,24 @@
from PIL import Image, ImageFilter from PIL import Image, ImageFilter
import ffmpeg import ffmpeg
import os import os
from processing.utils import generate_name, get_date_text
def images_to_video(directory, fps): def images_to_video(directory, fps, img_ext, img_name_format, video_name, video_ext, video_dir):
glob_path = os.path.join(directory, "*.png") images_pattern = os.path.join(directory, f"{img_name_format}.{img_ext}")
video_path = os.path.join(directory, "video.mp4")
ffmpeg.input(glob_path, pattern_type="glob", if video_dir:
framerate=fps).output(video_path).run() video_path = os.path.join(video_dir, f"{video_name}.{video_ext}")
else:
video_path = generate_name(
extension=video_ext, name=video_name, subfolder="videos")
ffmpeg.input(images_pattern,
framerate=fps).output(video_path, pix_fmt='yuv420p').global_args("-y").run()
def video_to_images(video_path, img_ext):
images_pattern = generate_name(
extension=img_ext, name=video_path, subfolder=os.path.join("images", get_date_text()))
ffmpeg.input(video_path).output(images_pattern).run()

View file

@ -1,12 +1,6 @@
from methods.stack_methods import denoise, startracks, noise_extractor, untrack from methods.stack_methods import denoise, startracks, noise_extractor, untrack
import os import os
import datetime from processing.utils import generate_name
def generate_name():
os.makedirs("./output/stacked", exist_ok=True)
return f"./output/stacked/{datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.png"
def stacking(dir, method): def stacking(dir, method):

16
processing/utils.py Normal file
View file

@ -0,0 +1,16 @@
import os
import datetime
def generate_name(name=False, subfolder="stacked", extension="png", format='%Y-%m-%d_%H-%M-%S'):
os.makedirs(os.path.join("./output", subfolder), exist_ok=True)
if name is False or name == "":
name = get_date_text(format)
return os.path.join("./output", subfolder, f"{name}.{extension}")
def get_date_text(format='%Y-%m-%d_%H-%M-%S'):
return datetime.datetime.now().strftime(format)

View file

@ -2,16 +2,29 @@ import gradio as gr
from processing.bulk import images_to_video from processing.bulk import images_to_video
with gr.Blocks() as app: with gr.Blocks() as app:
gr.Markdown("Convert images to video. # **WIP, not working**") gr.Markdown("Convert images to video.")
with gr.Row(): directory = gr.Text(
with gr.Column(): placeholder="A directory with many images of the same size", label="Directory")
directory = gr.Text(
placeholder="A directory with many images of the same size", lines=1, label="Directory")
fps = gr.Number(label="FPS") fps = gr.Number(label="FPS", value=30, min=0)
submit = gr.Button("Submit")
with gr.Accordion("Advanced settings", open=False) as acc:
video_name = gr.Text(
label="Video name", placeholder="Video name e.g. video. If not specified will be generated by time")
video_ext = gr.Dropdown(label="Video extension", choices=[
"mp4"], value="mp4")
video_dir = gr.Text(
label="Video directory", placeholder="The directory where the video will be saved. If not specified `./output/videos`")
img_ext = gr.Dropdown(label="Image extension", choices=[
"png", "jpg"], value="png")
img_name_format = gr.Text(
label="Image name format", placeholder="ffmpeg pattern e.g. %04d is (0000.png)", value="%d")
submit = gr.Button("Submit")
submit.click( submit.click(
fn=images_to_video, fn=images_to_video,
inputs=[directory, fps], inputs=[directory, fps, img_ext, img_name_format,
video_name, video_ext, video_dir],
) )