fffiloni commited on
Commit
c93a0cb
1 Parent(s): 07f7f7b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -12
app.py CHANGED
@@ -1,5 +1,9 @@
1
  import gradio as gr
2
  import os
 
 
 
 
3
 
4
  from huggingface_hub import snapshot_download
5
 
@@ -13,9 +17,7 @@ for model_id in model_ids:
13
  model_name = model_id.split('/')[-1]
14
  snapshot_download(model_id, local_dir=f'checkpoints/{model_name}')
15
 
16
- import subprocess
17
 
18
- import cv2
19
 
20
  def get_frame_count_in_duration(filepath):
21
  video = cv2.VideoCapture(filepath)
@@ -28,29 +30,92 @@ def get_frame_count_in_duration(filepath):
28
  return gr.update(maximum=frame_count)
29
 
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  def run_inference(prompt, video_path, condition, video_length):
33
 
34
  output_path = 'output/'
35
  os.makedirs(output_path, exist_ok=True)
36
 
37
- # Construct the video path
38
- video_path_output = os.path.join(output_path, f"{prompt}.mp4")
39
 
40
  # Check if the file already exists
41
  if os.path.exists(video_path_output):
42
  # Delete the existing file
43
  os.remove(video_path_output)
44
 
45
- if video_length > 12:
46
- command = f"python inference.py --prompt '{prompt}' --condition '{condition}' --video_path '{video_path}' --output_path '{output_path}' --video_length {video_length} --smoother_steps 19 20 --is_long_video"
47
- else:
48
- command = f"python inference.py --prompt '{prompt}' --condition '{condition}' --video_path '{video_path}' --output_path '{output_path}' --video_length {video_length} --smoother_steps 19 20"
49
- subprocess.run(command, shell=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
- # Construct the video path
52
- video_path_output = os.path.join(output_path, f"{prompt}.mp4")
53
- return "done", video_path_output
54
 
55
 
56
  with gr.Blocks() as demo:
 
1
  import gradio as gr
2
  import os
3
+ import subprocess
4
+ import cv2
5
+ from moviepy.editor import VideoFileClip, concatenate_videoclips
6
+ import math
7
 
8
  from huggingface_hub import snapshot_download
9
 
 
17
  model_name = model_id.split('/')[-1]
18
  snapshot_download(model_id, local_dir=f'checkpoints/{model_name}')
19
 
 
20
 
 
21
 
22
  def get_frame_count_in_duration(filepath):
23
  video = cv2.VideoCapture(filepath)
 
30
  return gr.update(maximum=frame_count)
31
 
32
 
33
+ # Function to split video into chunks
34
+ def split_video_into_chunks(video_path, chunk_size):
35
+ # Load the video clip
36
+ video = VideoFileClip(video_path)
37
+
38
+ # Calculate the total number of frames
39
+ total_frames = int(video.duration * video.fps)
40
+
41
+ # Calculate the number of chunks needed
42
+ num_chunks = math.ceil(total_frames / chunk_size)
43
+
44
+ # Create a list to store the chunks
45
+ chunks = []
46
+
47
+ # Split the video into chunks
48
+ for i in range(num_chunks):
49
+ # Calculate the start and end frame for the chunk
50
+ start_frame = i * chunk_size
51
+ end_frame = min((i + 1) * chunk_size, total_frames)
52
+
53
+ # Extract the chunk from the video
54
+ chunk = video.subclip(start_frame / video.fps, end_frame / video.fps)
55
+
56
+ # Add the chunk to the list
57
+ chunks.append(chunk)
58
+
59
+ # If the last chunk is smaller than the chunk size
60
+ if len(chunks) > 0 and len(chunks[-1]) < chunk_size:
61
+ # Adjust the end frame of the last chunk to the total frames
62
+ chunks[-1] = video.subclip(chunks[-1].t_start, video.duration)
63
+
64
+ return chunks
65
+
66
+
67
+
68
 
69
  def run_inference(prompt, video_path, condition, video_length):
70
 
71
  output_path = 'output/'
72
  os.makedirs(output_path, exist_ok=True)
73
 
74
+ # Construct the final video path
75
+ video_path_output = 'final_video.mp4'
76
 
77
  # Check if the file already exists
78
  if os.path.exists(video_path_output):
79
  # Delete the existing file
80
  os.remove(video_path_output)
81
 
82
+ # Specify the path to your video file
83
+ video_path = video_path
84
+
85
+ # Specify the maximum number of frames per chunk
86
+ chunk_size = 12
87
+
88
+ # Split the video into chunks
89
+ video_chunks = split_video_into_chunks(video_path, chunk_size)
90
+
91
+ # Process each chunk and store the processed chunk filenames
92
+ processed_chunk_filenames = []
93
+ for i, chunk in enumerate(video_chunks):
94
+ # Count the frame number of the video chunk
95
+ frame_count = len(chunk)
96
+ command = f"python inference.py --prompt '{prompt}' --condition '{condition}' --video_path '{chunk}' --output_path '{output_path}' --video_length {frame_count} --smoother_steps 19 20"
97
+ subprocess.run(command, shell=True)
98
+
99
+ # Construct the video path
100
+ video_path_output = os.path.join(output_path, f"{prompt}_{i}.mp4")
101
+
102
+ processed_chunk_filename = video_path_output
103
+ processed_chunk_filenames.append(processed_chunk_filename)
104
+
105
+ # Load the processed video chunks
106
+ processed_chunks = [VideoFileClip(filename) for filename in processed_chunk_filenames]
107
+
108
+ # Concatenate the processed video chunks into a final video
109
+ final_video = concatenate_videoclips(processed_chunks)
110
+
111
+ # Export the final video to a file
112
+ final_video.write_videofile('final_video.mp4')
113
+
114
+ # Clean up the temporary processed chunk files (optional)
115
+ for filename in processed_chunk_filenames:
116
+ os.remove(filename)
117
 
118
+ return "done", 'final_video.mp4'
 
 
119
 
120
 
121
  with gr.Blocks() as demo: