Update app.py
Browse files
app.py
CHANGED
|
@@ -15,7 +15,6 @@ B_KEY = os.getenv("B_KEY")
|
|
| 15 |
|
| 16 |
# URLs
|
| 17 |
API_URL = os.getenv("API_URL")
|
| 18 |
-
UPLOAD_URL = os.getenv("UPLOAD_URL")
|
| 19 |
|
| 20 |
def lipsync_api_call(video_url, audio_url):
|
| 21 |
headers = {
|
|
@@ -53,31 +52,27 @@ def check_job_status(job_id):
|
|
| 53 |
return None
|
| 54 |
|
| 55 |
def get_media_duration(file_path):
|
| 56 |
-
# Fetch media duration using ffprobe
|
| 57 |
cmd = ['ffprobe', '-v', 'error', '-show_entries', 'format=duration', '-of', 'default=noprint_wrappers=1:nokey=1', file_path]
|
| 58 |
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
| 59 |
return float(result.stdout.strip())
|
| 60 |
|
| 61 |
def combine_audio_video(video_path, audio_path, output_path):
|
| 62 |
-
# Get durations of both video and audio
|
| 63 |
video_duration = get_media_duration(video_path)
|
| 64 |
audio_duration = get_media_duration(audio_path)
|
| 65 |
|
| 66 |
if video_duration > audio_duration:
|
| 67 |
-
# Trim video to match the audio length
|
| 68 |
cmd = [
|
| 69 |
'ffmpeg', '-i', video_path, '-i', audio_path,
|
| 70 |
-
'-t', str(audio_duration),
|
| 71 |
'-map', '0:v', '-map', '1:a',
|
| 72 |
'-c:v', 'copy', '-c:a', 'aac',
|
| 73 |
'-y', output_path
|
| 74 |
]
|
| 75 |
else:
|
| 76 |
-
|
| 77 |
-
loop_count = int(audio_duration // video_duration) + 1 # Calculate how many times to loop
|
| 78 |
cmd = [
|
| 79 |
'ffmpeg', '-stream_loop', str(loop_count), '-i', video_path, '-i', audio_path,
|
| 80 |
-
'-t', str(audio_duration),
|
| 81 |
'-map', '0:v', '-map', '1:a',
|
| 82 |
'-c:v', 'copy', '-c:a', 'aac',
|
| 83 |
'-shortest', '-y', output_path
|
|
@@ -91,7 +86,7 @@ def process_video(video_url, audio_url, progress=gr.Progress()):
|
|
| 91 |
if not video_url:
|
| 92 |
return None, "No video URL provided"
|
| 93 |
|
| 94 |
-
session_id = str(uuid.uuid4())
|
| 95 |
|
| 96 |
progress(0.2, desc="Processing video...")
|
| 97 |
|
|
@@ -121,13 +116,11 @@ def process_video(video_url, audio_url, progress=gr.Progress()):
|
|
| 121 |
except Exception as e:
|
| 122 |
progress(0.8, desc="Falling back to simple combination...")
|
| 123 |
try:
|
| 124 |
-
# Download the video from the URL
|
| 125 |
video_response = requests.get(video_url)
|
| 126 |
temp_video_path = f"temp_video_{session_id}.mp4"
|
| 127 |
with open(temp_video_path, "wb") as f:
|
| 128 |
f.write(video_response.content)
|
| 129 |
|
| 130 |
-
# Download the audio from the URL
|
| 131 |
audio_response = requests.get(audio_url)
|
| 132 |
temp_audio_path = f"temp_audio_{session_id}.mp3"
|
| 133 |
with open(temp_audio_path, "wb") as f:
|
|
@@ -136,7 +129,6 @@ def process_video(video_url, audio_url, progress=gr.Progress()):
|
|
| 136 |
output_path = f"output_{session_id}.mp4"
|
| 137 |
combine_audio_video(temp_video_path, temp_audio_path, output_path)
|
| 138 |
|
| 139 |
-
# Clean up temporary files
|
| 140 |
os.remove(temp_video_path)
|
| 141 |
os.remove(temp_audio_path)
|
| 142 |
|
|
@@ -157,15 +149,8 @@ def create_interface():
|
|
| 157 |
video_output = gr.Video(label="Generated Video")
|
| 158 |
status_output = gr.Textbox(label="Status", interactive=False)
|
| 159 |
|
| 160 |
-
def on_generate(video_url, audio_url):
|
| 161 |
-
if not audio_url:
|
| 162 |
-
return None, "Please provide an audio URL."
|
| 163 |
-
if not video_url:
|
| 164 |
-
return None, "Please provide a video URL."
|
| 165 |
-
return process_video(video_url, audio_url)
|
| 166 |
-
|
| 167 |
generate_btn.click(
|
| 168 |
-
fn=
|
| 169 |
inputs=[video_url_input, audio_url_input],
|
| 170 |
outputs=[video_output, status_output]
|
| 171 |
)
|
|
|
|
| 15 |
|
| 16 |
# URLs
|
| 17 |
API_URL = os.getenv("API_URL")
|
|
|
|
| 18 |
|
| 19 |
def lipsync_api_call(video_url, audio_url):
|
| 20 |
headers = {
|
|
|
|
| 52 |
return None
|
| 53 |
|
| 54 |
def get_media_duration(file_path):
|
|
|
|
| 55 |
cmd = ['ffprobe', '-v', 'error', '-show_entries', 'format=duration', '-of', 'default=noprint_wrappers=1:nokey=1', file_path]
|
| 56 |
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
| 57 |
return float(result.stdout.strip())
|
| 58 |
|
| 59 |
def combine_audio_video(video_path, audio_path, output_path):
|
|
|
|
| 60 |
video_duration = get_media_duration(video_path)
|
| 61 |
audio_duration = get_media_duration(audio_path)
|
| 62 |
|
| 63 |
if video_duration > audio_duration:
|
|
|
|
| 64 |
cmd = [
|
| 65 |
'ffmpeg', '-i', video_path, '-i', audio_path,
|
| 66 |
+
'-t', str(audio_duration),
|
| 67 |
'-map', '0:v', '-map', '1:a',
|
| 68 |
'-c:v', 'copy', '-c:a', 'aac',
|
| 69 |
'-y', output_path
|
| 70 |
]
|
| 71 |
else:
|
| 72 |
+
loop_count = int(audio_duration // video_duration) + 1
|
|
|
|
| 73 |
cmd = [
|
| 74 |
'ffmpeg', '-stream_loop', str(loop_count), '-i', video_path, '-i', audio_path,
|
| 75 |
+
'-t', str(audio_duration),
|
| 76 |
'-map', '0:v', '-map', '1:a',
|
| 77 |
'-c:v', 'copy', '-c:a', 'aac',
|
| 78 |
'-shortest', '-y', output_path
|
|
|
|
| 86 |
if not video_url:
|
| 87 |
return None, "No video URL provided"
|
| 88 |
|
| 89 |
+
session_id = str(uuid.uuid4())
|
| 90 |
|
| 91 |
progress(0.2, desc="Processing video...")
|
| 92 |
|
|
|
|
| 116 |
except Exception as e:
|
| 117 |
progress(0.8, desc="Falling back to simple combination...")
|
| 118 |
try:
|
|
|
|
| 119 |
video_response = requests.get(video_url)
|
| 120 |
temp_video_path = f"temp_video_{session_id}.mp4"
|
| 121 |
with open(temp_video_path, "wb") as f:
|
| 122 |
f.write(video_response.content)
|
| 123 |
|
|
|
|
| 124 |
audio_response = requests.get(audio_url)
|
| 125 |
temp_audio_path = f"temp_audio_{session_id}.mp3"
|
| 126 |
with open(temp_audio_path, "wb") as f:
|
|
|
|
| 129 |
output_path = f"output_{session_id}.mp4"
|
| 130 |
combine_audio_video(temp_video_path, temp_audio_path, output_path)
|
| 131 |
|
|
|
|
| 132 |
os.remove(temp_video_path)
|
| 133 |
os.remove(temp_audio_path)
|
| 134 |
|
|
|
|
| 149 |
video_output = gr.Video(label="Generated Video")
|
| 150 |
status_output = gr.Textbox(label="Status", interactive=False)
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
generate_btn.click(
|
| 153 |
+
fn=process_video,
|
| 154 |
inputs=[video_url_input, audio_url_input],
|
| 155 |
outputs=[video_output, status_output]
|
| 156 |
)
|