Your Name commited on
Commit
2e38275
1 Parent(s): aac6c7c

Add overlays

Browse files
Files changed (3) hide show
  1. animate.py +11 -3
  2. app.py +25 -10
  3. test.ipynb +29 -1
animate.py CHANGED
@@ -9,7 +9,7 @@ import numpy as np
9
  from frame_interpolation.eval import interpolator, util
10
  from huggingface_hub import snapshot_download
11
  from image_tools.sizes import resize_and_crop
12
- from moviepy.editor import CompositeVideoClip
13
  from moviepy.editor import VideoFileClip as vfc
14
  from PIL import Image
15
 
@@ -210,7 +210,9 @@ def get_video_frames(
210
  return frames, cv2_images
211
 
212
 
213
- def create_mp4_with_audio(frames, cv2_images, duration, audio, output_path):
 
 
214
  vid_output_dir = output_path.parent
215
  temp_vid_path = Path(vid_output_dir / "TEMP.mp4")
216
  mediapy.write_video(temp_vid_path, frames, fps=10)
@@ -234,6 +236,12 @@ def create_mp4_with_audio(frames, cv2_images, duration, audio, output_path):
234
 
235
  new_clip = CompositeVideoClip([clip1, clip2, clip3])
236
  new_clip.audio = audio # Naviely append audio without considering the length of the video, could be a problem, no idea, but it works, so I'm not touching it
 
 
 
 
237
  new_clip.set_duration(duration)
238
- new_clip.write_videofile(output_path.as_posix(), audio_codec="aac")
 
 
239
  return output_path.as_posix()
 
9
  from frame_interpolation.eval import interpolator, util
10
  from huggingface_hub import snapshot_download
11
  from image_tools.sizes import resize_and_crop
12
+ from moviepy.editor import CompositeVideoClip, ImageClip
13
  from moviepy.editor import VideoFileClip as vfc
14
  from PIL import Image
15
 
 
210
  return frames, cv2_images
211
 
212
 
213
+ def create_mp4_with_audio(
214
+ frames, cv2_images, duration, audio, output_path, overlay_image
215
+ ):
216
  vid_output_dir = output_path.parent
217
  temp_vid_path = Path(vid_output_dir / "TEMP.mp4")
218
  mediapy.write_video(temp_vid_path, frames, fps=10)
 
236
 
237
  new_clip = CompositeVideoClip([clip1, clip2, clip3])
238
  new_clip.audio = audio # Naviely append audio without considering the length of the video, could be a problem, no idea, but it works, so I'm not touching it
239
+ image = (
240
+ ImageClip(overlay_image).set_duration(duration).resize(0.5).set_pos("center")
241
+ )
242
+
243
  new_clip.set_duration(duration)
244
+ # Now overlay the image with moviepy
245
+ final_clip = CompositeVideoClip([new_clip, image])
246
+ final_clip.write_videofile(output_path.as_posix(), audio_codec="aac")
247
  return output_path.as_posix()
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import os
 
2
  import shutil
3
  import subprocess
4
  import sys
@@ -6,17 +7,14 @@ import uuid
6
  from pathlib import Path
7
 
8
  import gradio as gr
9
- from moviepy.editor import AudioFileClip
10
-
11
-
12
  import opengraph
13
- import pathlib
14
-
15
  import requests
 
16
 
17
  output_dir = Path("temp/").absolute()
18
  output_dir.mkdir(exist_ok=True, parents=True)
19
 
 
20
  class SpotifyApi:
21
  spotify_directory = Path(output_dir / "spotify")
22
 
@@ -52,6 +50,7 @@ class SpotifyApi:
52
  r = requests.get(image, allow_redirects=True)
53
  path = self.perma_output_path.with_suffix(".jpg").absolute()
54
  open(path, "wb").write(r.content)
 
55
 
56
  def get_title(self) -> str:
57
  return self.opengraph["title"]
@@ -81,12 +80,15 @@ def process_inputs(
81
  if spotify_url:
82
  spotify = SpotifyApi(spotify_url)
83
  audio_input.path = spotify.download_episode()
 
84
  images = get_stable_diffusion_images(prompt)
85
- video = animate_images(images, audio_input)
86
  return video
87
 
88
 
89
- def animate_images(image_paths: list[str], audio_input: AudioInput) -> str:
 
 
90
  from animate import ( # Only import after git clone and when necessary takes loooong
91
  create_mp4_with_audio,
92
  get_video_frames,
@@ -103,7 +105,12 @@ def animate_images(image_paths: list[str], audio_input: AudioInput) -> str:
103
  video_frames, cv2_images = get_video_frames(image_paths, vid_output_dir)
104
  path = Path(vid_output_dir / "output_final.mp4")
105
  return create_mp4_with_audio(
106
- video_frames, cv2_images, audio_clip.duration, audio_clip, path
 
 
 
 
 
107
  )
108
 
109
 
@@ -123,7 +130,15 @@ iface = gr.Interface(
123
  gr.Number(label="Run for (in seconds)"),
124
  ],
125
  outputs="video",
126
- examples = [["A podcast clip", None, "https://open.spotify.com/episode/31u9tI8t5IFrdv3QhZtPHI",50,60]]
 
 
 
 
 
 
 
 
127
  )
128
 
129
  if __name__ == "__main__":
@@ -141,4 +156,4 @@ if __name__ == "__main__":
141
  os.chdir(
142
  output_dir
143
  ) # change working directory to output_dir because the hf spaces model has no option to specify output directory ¯\_(ツ)_/¯
144
- iface.launch(share=True)
 
1
  import os
2
+ import pathlib
3
  import shutil
4
  import subprocess
5
  import sys
 
7
  from pathlib import Path
8
 
9
  import gradio as gr
 
 
 
10
  import opengraph
 
 
11
  import requests
12
+ from moviepy.editor import AudioFileClip
13
 
14
  output_dir = Path("temp/").absolute()
15
  output_dir.mkdir(exist_ok=True, parents=True)
16
 
17
+
18
  class SpotifyApi:
19
  spotify_directory = Path(output_dir / "spotify")
20
 
 
50
  r = requests.get(image, allow_redirects=True)
51
  path = self.perma_output_path.with_suffix(".jpg").absolute()
52
  open(path, "wb").write(r.content)
53
+ return path
54
 
55
  def get_title(self) -> str:
56
  return self.opengraph["title"]
 
80
  if spotify_url:
81
  spotify = SpotifyApi(spotify_url)
82
  audio_input.path = spotify.download_episode()
83
+ spotify_image = spotify.download_image()
84
  images = get_stable_diffusion_images(prompt)
85
+ video = animate_images(images, audio_input, spotify_image)
86
  return video
87
 
88
 
89
+ def animate_images(
90
+ image_paths: list[str], audio_input: AudioInput, overlay_image_path: str
91
+ ) -> str:
92
  from animate import ( # Only import after git clone and when necessary takes loooong
93
  create_mp4_with_audio,
94
  get_video_frames,
 
105
  video_frames, cv2_images = get_video_frames(image_paths, vid_output_dir)
106
  path = Path(vid_output_dir / "output_final.mp4")
107
  return create_mp4_with_audio(
108
+ video_frames,
109
+ cv2_images,
110
+ audio_clip.duration,
111
+ audio_clip,
112
+ path,
113
+ overlay_image_path.as_posix(),
114
  )
115
 
116
 
 
130
  gr.Number(label="Run for (in seconds)"),
131
  ],
132
  outputs="video",
133
+ examples=[
134
+ [
135
+ "A podcast clip",
136
+ None,
137
+ "https://open.spotify.com/episode/31u9tI8t5IFrdv3QhZtPHI",
138
+ 50,
139
+ 60,
140
+ ]
141
+ ],
142
  )
143
 
144
  if __name__ == "__main__":
 
156
  os.chdir(
157
  output_dir
158
  ) # change working directory to output_dir because the hf spaces model has no option to specify output directory ¯\_(ツ)_/¯
159
+ iface.launch()
test.ipynb CHANGED
@@ -36,6 +36,29 @@
36
  "test_spotify_downloads()"
37
  ]
38
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  {
40
  "cell_type": "code",
41
  "execution_count": 1,
@@ -68,7 +91,7 @@
68
  ],
69
  "metadata": {
70
  "kernelspec": {
71
- "display_name": "Python 3 (ipykernel)",
72
  "language": "python",
73
  "name": "python3"
74
  },
@@ -83,6 +106,11 @@
83
  "nbconvert_exporter": "python",
84
  "pygments_lexer": "ipython3",
85
  "version": "3.9.13"
 
 
 
 
 
86
  }
87
  },
88
  "nbformat": 4,
 
36
  "test_spotify_downloads()"
37
  ]
38
  },
39
+ {
40
+ "cell_type": "code",
41
+ "execution_count": 4,
42
+ "id": "237e3903",
43
+ "metadata": {},
44
+ "outputs": [
45
+ {
46
+ "data": {
47
+ "text/plain": [
48
+ "<moviepy.video.VideoClip.ImageClip at 0x7f5951ec8700>"
49
+ ]
50
+ },
51
+ "execution_count": 4,
52
+ "metadata": {},
53
+ "output_type": "execute_result"
54
+ }
55
+ ],
56
+ "source": [
57
+ "from moviepy.editor import ImageClip\n",
58
+ "\n",
59
+ "ImageClip(img=\"/notebooks/stablepod/temp/1ae3a379.jpg\")"
60
+ ]
61
+ },
62
  {
63
  "cell_type": "code",
64
  "execution_count": 1,
 
91
  ],
92
  "metadata": {
93
  "kernelspec": {
94
+ "display_name": "Python 3.9.6 64-bit",
95
  "language": "python",
96
  "name": "python3"
97
  },
 
106
  "nbconvert_exporter": "python",
107
  "pygments_lexer": "ipython3",
108
  "version": "3.9.13"
109
+ },
110
+ "vscode": {
111
+ "interpreter": {
112
+ "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
113
+ }
114
  }
115
  },
116
  "nbformat": 4,