spookyuser
commited on
Commit
•
8e972b4
1
Parent(s):
d740874
Add start time and end time
Browse files
app.py
CHANGED
@@ -1,11 +1,11 @@
|
|
1 |
-
import gradio as gr
|
2 |
import os
|
3 |
-
|
4 |
-
from pathlib import Path
|
5 |
import subprocess
|
6 |
import uuid
|
7 |
-
import
|
8 |
|
|
|
|
|
9 |
|
10 |
output_dir = Path("temp/").absolute()
|
11 |
output_dir.mkdir(exist_ok=True, parents=True)
|
@@ -14,6 +14,8 @@ os.chdir(
|
|
14 |
output_dir
|
15 |
) # change working directory to output_dir because the hf spaces model has no option to specify output directory ¯\_(ツ)_/¯
|
16 |
|
|
|
|
|
17 |
|
18 |
class SpotifyApi:
|
19 |
spotify_directory = Path("spotify")
|
@@ -62,16 +64,26 @@ class SpotifyApi:
|
|
62 |
return final_mp3.as_posix()
|
63 |
|
64 |
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
if spotify_url:
|
67 |
spotify = SpotifyApi()
|
68 |
-
|
69 |
image = get_stable_diffusion_image(prompt)
|
70 |
-
video = add_static_image_to_audio(image,
|
71 |
return video
|
72 |
|
73 |
|
74 |
-
def add_static_image_to_audio(image,
|
75 |
"""Create and save a video file to `output_path` after
|
76 |
combining a static image that is located in `image_path`
|
77 |
with an audio file in `audio_path`"""
|
@@ -80,10 +92,16 @@ def add_static_image_to_audio(image, audio) -> str:
|
|
80 |
vid_output_dir = Path(output_dir / foldername)
|
81 |
vid_output_dir.mkdir(exist_ok=True, parents=True)
|
82 |
os.chdir(vid_output_dir)
|
83 |
-
audio_clip = AudioFileClip(
|
|
|
|
|
|
|
|
|
84 |
image_clip = ImageClip(image)
|
85 |
video_clip = image_clip.set_audio(audio_clip)
|
86 |
-
video_clip.duration =
|
|
|
|
|
87 |
video_clip.fps = 1
|
88 |
path = Path("output.mp4").as_posix()
|
89 |
video_clip.write_videofile(path)
|
@@ -102,6 +120,8 @@ iface = gr.Interface(
|
|
102 |
gr.Textbox(label="Describe your podcast clip"),
|
103 |
gr.Audio(type="filepath", label="Upload an mp3"),
|
104 |
gr.Textbox(label="Or Paste a spotify episode link"),
|
|
|
|
|
105 |
],
|
106 |
outputs="video",
|
107 |
)
|
|
|
|
|
1 |
import os
|
2 |
+
import shutil
|
|
|
3 |
import subprocess
|
4 |
import uuid
|
5 |
+
from pathlib import Path
|
6 |
|
7 |
+
import gradio as gr
|
8 |
+
from moviepy.editor import AudioFileClip, ImageClip
|
9 |
|
10 |
output_dir = Path("temp/").absolute()
|
11 |
output_dir.mkdir(exist_ok=True, parents=True)
|
|
|
14 |
output_dir
|
15 |
) # change working directory to output_dir because the hf spaces model has no option to specify output directory ¯\_(ツ)_/¯
|
16 |
|
17 |
+
# TODO: Add an if statement that checks if a gpu is available, if one is then do weird stable diffusion stuff, if one isn't, then just use the regular hugging face api
|
18 |
+
|
19 |
|
20 |
class SpotifyApi:
|
21 |
spotify_directory = Path("spotify")
|
|
|
64 |
return final_mp3.as_posix()
|
65 |
|
66 |
|
67 |
+
class AudioInput:
|
68 |
+
def __init__(self, path: str, start_time: int, run_for: int):
|
69 |
+
self.path = path
|
70 |
+
self.start_time = start_time
|
71 |
+
self.run_for = run_for
|
72 |
+
|
73 |
+
|
74 |
+
def process_inputs(
|
75 |
+
prompt: str, audio_path: str, spotify_url: str, start_time: int, run_for: int
|
76 |
+
) -> str:
|
77 |
+
audio_input = AudioInput(audio_path, start_time, run_for)
|
78 |
if spotify_url:
|
79 |
spotify = SpotifyApi()
|
80 |
+
audio_input.path = spotify.download_episode(spotify_url)
|
81 |
image = get_stable_diffusion_image(prompt)
|
82 |
+
video = add_static_image_to_audio(image, audio_input)
|
83 |
return video
|
84 |
|
85 |
|
86 |
+
def add_static_image_to_audio(image, audio_input) -> str:
|
87 |
"""Create and save a video file to `output_path` after
|
88 |
combining a static image that is located in `image_path`
|
89 |
with an audio file in `audio_path`"""
|
|
|
92 |
vid_output_dir = Path(output_dir / foldername)
|
93 |
vid_output_dir.mkdir(exist_ok=True, parents=True)
|
94 |
os.chdir(vid_output_dir)
|
95 |
+
audio_clip = AudioFileClip(audio_input.path)
|
96 |
+
# Make the audio clip start at the specified time and set the duration to the specified duration
|
97 |
+
audio_clip = audio_clip.subclip(
|
98 |
+
audio_input.start_time, audio_input.start_time + audio_input.run_for
|
99 |
+
)
|
100 |
image_clip = ImageClip(image)
|
101 |
video_clip = image_clip.set_audio(audio_clip)
|
102 |
+
video_clip.duration = (
|
103 |
+
audio_clip.duration
|
104 |
+
) # The duration here is the cut duration from above
|
105 |
video_clip.fps = 1
|
106 |
path = Path("output.mp4").as_posix()
|
107 |
video_clip.write_videofile(path)
|
|
|
120 |
gr.Textbox(label="Describe your podcast clip"),
|
121 |
gr.Audio(type="filepath", label="Upload an mp3"),
|
122 |
gr.Textbox(label="Or Paste a spotify episode link"),
|
123 |
+
gr.Number(label="Start time (in seconds)"),
|
124 |
+
gr.Number(label="Run for (in seconds)"),
|
125 |
],
|
126 |
outputs="video",
|
127 |
)
|