Spanicin commited on
Commit
6cf745b
·
verified ·
1 Parent(s): e12f166

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -1
app.py CHANGED
@@ -1,7 +1,8 @@
1
- import argparse
2
  import tempfile
3
  import os
4
  import shutil
 
 
5
  from flask import Flask, request, jsonify, send_from_directory
6
  from omegaconf import OmegaConf
7
  import torch
@@ -107,6 +108,43 @@ def generate_audio(voice_cloning, text_prompt):
107
 
108
  return driven_audio_path
109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
  @app.route('/run', methods=['POST'])
112
  def generate_video():
@@ -136,6 +174,8 @@ def generate_video():
136
  # You can pass additional parameters via form data if needed (e.g., checkpoint path)
137
  inference_ckpt_path = request.form.get('inference_ckpt_path', 'checkpoints/latentsync_unet.pt')
138
  unet_config_path = request.form.get('unet_config_path', 'configs/unet/second_stage.yaml')
 
 
139
 
140
  try:
141
  run_inference(
 
 
1
  import tempfile
2
  import os
3
  import shutil
4
+ import librosa
5
+ import json
6
  from flask import Flask, request, jsonify, send_from_directory
7
  from omegaconf import OmegaConf
8
  import torch
 
108
 
109
  return driven_audio_path
110
 
111
+ def get_video_duration(video_path):
112
+ """Extracts video duration dynamically using ffprobe."""
113
+ cmd = [
114
+ "ffprobe", "-v", "error", "-show_entries", "format=duration",
115
+ "-of", "json", video_path
116
+ ]
117
+ result = subprocess.run(cmd, capture_output=True, text=True)
118
+ duration = json.loads(result.stdout)["format"]["duration"]
119
+ return float(duration)
120
+
121
+
122
+ def extend_video_simple(video_path, audio_path, output_path):
123
+ # Get durations dynamically
124
+ audio_duration = librosa.get_duration(path=audio_path)
125
+ video_duration = get_video_duration(video_path)
126
+
127
+ print(f"Video Duration: {video_duration:.2f} sec")
128
+ print(f"Audio Duration: {audio_duration:.2f} sec")
129
+
130
+ if audio_duration > video_duration:
131
+ print("Extending video by adding reversed version.")
132
+
133
+ # Create a reversed version of the full video
134
+ reversed_clip = "reversed.mp4"
135
+ subprocess.run(
136
+ f"ffmpeg -i {video_path} -vf reverse -an {reversed_clip}", shell=True
137
+ )
138
+
139
+ # Merge original + reversed
140
+ subprocess.run(
141
+ f"ffmpeg -i {video_path} -i {reversed_clip} -filter_complex \"[0:v:0][1:v:0]concat=n=2:v=1[outv]\" -map \"[outv]\" -an {output_path}",
142
+ shell=True
143
+ )
144
+
145
+ else:
146
+ print("Audio is not longer than video. No extension needed.")
147
+ subprocess.run(f"cp {video_path} {output_path}", shell=True)
148
 
149
  @app.route('/run', methods=['POST'])
150
  def generate_video():
 
174
  # You can pass additional parameters via form data if needed (e.g., checkpoint path)
175
  inference_ckpt_path = request.form.get('inference_ckpt_path', 'checkpoints/latentsync_unet.pt')
176
  unet_config_path = request.form.get('unet_config_path', 'configs/unet/second_stage.yaml')
177
+
178
+ extend_video_simple(temp_video_path, temp_audio_path, output_video)
179
 
180
  try:
181
  run_inference(