MCPcopy Create free account
hub / github.com/MeiGen-AI/MultiTalk / save_video_ffmpeg

Function save_video_ffmpeg

wan/utils/multitalk_utils.py:237–313  ·  view source on GitHub ↗
(gen_video_samples, save_path, vocal_audio_list, fps=25, quality=5, high_quality_save=False)

Source from the content-addressed store, hash-verified

235 return cache_file
236
237def save_video_ffmpeg(gen_video_samples, save_path, vocal_audio_list, fps=25, quality=5, high_quality_save=False):
238
239 def save_video(frames, save_path, fps, quality=9, ffmpeg_params=None):
240 writer = imageio.get_writer(
241 save_path, fps=fps, quality=quality, ffmpeg_params=ffmpeg_params
242 )
243 for frame in tqdm(frames, desc="Saving video"):
244 frame = np.array(frame)
245 writer.append_data(frame)
246 writer.close()
247 save_path_tmp = save_path + "-temp.mp4"
248
249 if high_quality_save:
250 cache_video(
251 tensor=gen_video_samples.unsqueeze(0),
252 save_file=save_path_tmp,
253 fps=fps,
254 nrow=1,
255 normalize=True,
256 value_range=(-1, 1)
257 )
258 else:
259 video_audio = (gen_video_samples+1)/2 # C T H W
260 video_audio = video_audio.permute(1, 2, 3, 0).cpu().numpy()
261 video_audio = np.clip(video_audio * 255, 0, 255).astype(np.uint8) # to [0, 255]
262 save_video(video_audio, save_path_tmp, fps=fps, quality=quality)
263
264
265 # crop audio according to video length
266 _, T, _, _ = gen_video_samples.shape
267 duration = T / fps
268 save_path_crop_audio = save_path + "-cropaudio.wav"
269 final_command = [
270 "ffmpeg",
271 "-i",
272 vocal_audio_list[0],
273 "-t",
274 f'{duration}',
275 save_path_crop_audio,
276 ]
277 subprocess.run(final_command, check=True)
278
279 save_path = save_path + ".mp4"
280 if high_quality_save:
281 final_command = [
282 "ffmpeg",
283 "-y",
284 "-i", save_path_tmp,
285 "-i", save_path_crop_audio,
286 "-c:v", "libx264",
287 "-crf", "0",
288 "-preset", "veryslow",
289 "-c:a", "aac",
290 "-shortest",
291 save_path,
292 ]
293 subprocess.run(final_command, check=True)
294 os.remove(save_path_tmp)

Callers 2

generateFunction · 0.90
generate_videoFunction · 0.90

Calls 2

save_videoFunction · 0.85
cache_videoFunction · 0.70

Tested by

no test coverage detected