fffiloni commited on
Commit
de1d7d7
·
1 Parent(s): 7d319ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -84
app.py CHANGED
@@ -29,54 +29,7 @@ def get_frame_count_in_duration(filepath):
29
  video.release()
30
  return gr.update(maximum=frame_count)
31
 
32
- # Function to process a video chunk and return the processed video
33
- def process_video_chunk(chunk, index):
34
- # Count the frame number of the video chunk
35
- frame_count = len(chunk)
36
 
37
- # Export the processed chunk to a file with the index in the file name
38
- processed_chunk_filename = f'processed_chunk_{index}.mp4'
39
- processed_chunk.write_videofile(processed_chunk_filename)
40
-
41
- return processed_chunk_filename, frame_count
42
-
43
- # Function to split video into chunks
44
- def split_video_into_chunks(video_path, chunk_size):
45
- # Load the video clip
46
- video = VideoFileClip(video_path)
47
-
48
- # Calculate the total number of frames
49
- total_frames = int(video.duration * video.fps)
50
-
51
- # Calculate the number of chunks needed
52
- num_chunks = math.ceil(total_frames / chunk_size)
53
-
54
- # Create a list to store the chunks and their frame counts
55
- chunks = []
56
-
57
- # Split the video into chunks
58
- for i in range(num_chunks):
59
- # Calculate the start and end frame for the chunk
60
- start_frame = i * chunk_size
61
- end_frame = min((i + 1) * chunk_size, total_frames)
62
-
63
- # Extract the chunk from the video
64
- chunk = video.subclip(start_frame / video.fps, end_frame / video.fps)
65
-
66
- # Calculate the frame count for the chunk
67
- frame_count = end_frame - start_frame
68
-
69
- # Add the chunk and its frame count to the list
70
- chunks.append((chunk, frame_count))
71
-
72
- # If the last chunk is smaller than the chunk size
73
- if len(chunks) > 0 and chunks[-1][1] < chunk_size:
74
- # Adjust the end frame of the last chunk to the total frames
75
- last_chunk = chunks[-1][0]
76
- last_frame_count = chunks[-1][1]
77
- chunks[-1] = (video.subclip(last_chunk.t_start, video.duration), last_frame_count)
78
-
79
- return chunks
80
 
81
 
82
 
@@ -87,52 +40,23 @@ def run_inference(prompt, video_path, condition, video_length):
87
  os.makedirs(output_path, exist_ok=True)
88
 
89
  # Construct the final video path
90
- video_path_output = 'final_video.mp4'
91
 
92
  # Check if the file already exists
93
  if os.path.exists(video_path_output):
94
  # Delete the existing file
95
  os.remove(video_path_output)
96
 
97
- # Specify the path to your video file
98
- video_path = video_path
99
-
100
- # Specify the maximum number of frames per chunk
101
- chunk_size = 12
102
-
103
- # Split the video into chunks
104
- video_chunks = split_video_into_chunks(video_path, chunk_size)
105
-
106
- # Process each chunk and store the processed chunk filenames and frame counts
107
- processed_chunk_info = []
108
- for i, (chunk, _) in enumerate(video_chunks):
109
- processed_chunk_filename, frame_count = process_video_chunk(chunk, i)
110
- # Count the frame number of the video chunk
111
- #frame_count = len(chunk)
112
- command = f"python inference.py --prompt '{prompt}' --condition '{condition}' --video_path '{chunk}' --output_path '{output_path}' --video_length {frame_count} --smoother_steps 19 20"
113
- subprocess.run(command, shell=True)
114
-
115
- # Construct the video path
116
- video_path_output = os.path.join(output_path, f"{prompt}_{i}.mp4")
117
-
118
- processed_chunk_filename = video_path_output
119
- #processed_chunk_filenames.append(processed_chunk_filename)
120
- processed_chunk_info.append((processed_chunk_filename, frame_count))
121
-
122
- # Load the processed video chunks
123
- processed_chunks = [VideoFileClip(filename) for filename, _ in processed_chunk_info]
124
-
125
- # Concatenate the processed video chunks into a final video
126
- final_video = concatenate_videoclips(processed_chunks)
127
 
128
- # Export the final video to a file
129
- final_video.write_videofile('final_video.mp4')
130
 
131
- # Clean up the temporary processed chunk files (optional)
132
- for filename, _ in processed_chunk_info:
133
- os.remove(filename)
134
 
135
- return "done", 'final_video.mp4'
136
 
137
 
138
  with gr.Blocks() as demo:
 
29
  video.release()
30
  return gr.update(maximum=frame_count)
31
 
 
 
 
 
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
 
35
 
 
40
  os.makedirs(output_path, exist_ok=True)
41
 
42
  # Construct the final video path
43
+ video_path_output = os.path.join(output_path, f"{prompt}.mp4"
44
 
45
  # Check if the file already exists
46
  if os.path.exists(video_path_output):
47
  # Delete the existing file
48
  os.remove(video_path_output)
49
 
50
+
51
+ command = f"python inference.py --prompt '{prompt}' --condition '{condition}' --video_path '{chunk}' --output_path '{output_path}' --video_length {video_length} --smoother_steps 19 20"
52
+ subprocess.run(command, shell=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
+ # Construct the video path
55
+ video_path_output = os.path.join(output_path, f"{prompt}.mp4")
56
 
57
+
 
 
58
 
59
+ return "done", video_path_output
60
 
61
 
62
  with gr.Blocks() as demo: