sheikhed commited on
Commit
a7c9b9d
·
verified ·
1 Parent(s): 89d75cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -7
app.py CHANGED
@@ -106,13 +106,37 @@ def check_job_status(job_id):
106
  time.sleep(10)
107
  return None
108
 
 
 
 
 
 
 
109
  def combine_audio_video(video_path, audio_path, output_path):
110
- cmd = [
111
- 'ffmpeg', '-i', video_path, '-i', audio_path,
112
- '-map', '0:v', '-map', '1:a',
113
- '-c:v', 'copy', '-c:a', 'aac',
114
- '-shortest', '-y', output_path
115
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  subprocess.run(cmd, check=True)
117
 
118
  def process_video(voice, model, text, progress=gr.Progress()):
@@ -201,4 +225,4 @@ def create_interface():
201
 
202
  if __name__ == "__main__":
203
  app = create_interface()
204
- app.launch()
 
106
  time.sleep(10)
107
  return None
108
 
109
+ def get_media_duration(file_path):
110
+ # Fetch media duration using ffprobe
111
+ cmd = ['ffprobe', '-v', 'error', '-show_entries', 'format=duration', '-of', 'default=noprint_wrappers=1:nokey=1', file_path]
112
+ result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
113
+ return float(result.stdout.strip())
114
+
115
  def combine_audio_video(video_path, audio_path, output_path):
116
+ # Get durations of both video and audio
117
+ video_duration = get_media_duration(video_path)
118
+ audio_duration = get_media_duration(audio_path)
119
+
120
+ if video_duration > audio_duration:
121
+ # Trim video to match the audio length
122
+ cmd = [
123
+ 'ffmpeg', '-i', video_path, '-i', audio_path,
124
+ '-t', str(audio_duration), # Trim video to audio duration
125
+ '-map', '0:v', '-map', '1:a',
126
+ '-c:v', 'copy', '-c:a', 'aac',
127
+ '-y', output_path
128
+ ]
129
+ else:
130
+ # Loop video if it's shorter than audio
131
+ loop_count = int(audio_duration // video_duration) + 1 # Calculate how many times to loop
132
+ cmd = [
133
+ 'ffmpeg', '-stream_loop', str(loop_count), '-i', video_path, '-i', audio_path,
134
+ '-t', str(audio_duration), # Match the duration of the final video with the audio
135
+ '-map', '0:v', '-map', '1:a',
136
+ '-c:v', 'copy', '-c:a', 'aac',
137
+ '-shortest', '-y', output_path
138
+ ]
139
+
140
  subprocess.run(cmd, check=True)
141
 
142
  def process_video(voice, model, text, progress=gr.Progress()):
 
225
 
226
  if __name__ == "__main__":
227
  app = create_interface()
228
+ app.launch()