diff --git a/routes/audio_mixing.py b/routes/audio_mixing.py index 6b7d10e..fc0cd03 100644 --- a/routes/audio_mixing.py +++ b/routes/audio_mixing.py @@ -42,9 +42,10 @@ def process_job(): if webhook_url: send_webhook(webhook_url, { "endpoint": "/audio-mixing", + "code": 200, "id": id, + "job_id": job_id, "response": gcs_url, - "code": 200, "message": "success" }) except Exception as e: @@ -52,12 +53,20 @@ def process_job(): if webhook_url: send_webhook(webhook_url, { "endpoint": "/audio-mixing", + "code": 500, "id": id, + "job_id": job_id, "response": None, - "code": 500, "message": str(e) }) # Start the thread with the app context passed threading.Thread(target=process_job, daemon=True).start() - return jsonify({"message": "processing"}), 202 \ No newline at end of file + return jsonify( + { + "code": 202, + "id": data.get("id"), + "job_id": job_id, + "message": "processing" + } + ), 202 \ No newline at end of file diff --git a/routes/transcribe.py b/routes/transcribe.py index 6438af2..53d5c90 100644 --- a/routes/transcribe.py +++ b/routes/transcribe.py @@ -28,12 +28,12 @@ def transcription_worker(): worker_thread = threading.Thread(target=transcription_worker, daemon=True) worker_thread.start() -def process_job(**kwargs): - media_url = kwargs['media_url'] - output = kwargs['output'] - webhook_url = kwargs['webhook_url'] - id = kwargs['id'] - job_id = kwargs['job_id'] +def process_job(media_url, output, webhook_url, id, job_id): + #media_url = kwargs['media_url'] + #output = kwargs['output'] + #webhook_url = kwargs['webhook_url'] + #id = kwargs['id'] + #job_id = kwargs['job_id'] try: logger.info(f"Job {job_id}: Starting transcription process for {media_url}") @@ -50,6 +50,8 @@ def process_job(**kwargs): "response": result, "message": "success" }) + else: + return result; except Exception as e: logger.error(f"Job {job_id}: Error during transcription - {e}") if webhook_url: @@ -112,7 +114,7 @@ def transcribe(): else: try: logger.info(f"Job {job_id}: No webhook provided, processing synchronously") - result = process_transcription(media_url, output) + result = process_job(media_url=media_url, output=output) logger.info(f"Job {job_id}: Returning transcription result") return jsonify({ "code": 200, diff --git a/services/audio_mixing.py b/services/audio_mixing.py index ddac99b..6a20aef 100644 --- a/services/audio_mixing.py +++ b/services/audio_mixing.py @@ -6,21 +6,56 @@ STORAGE_PATH = "/tmp/" def process_audio_mixing(video_url, audio_url, video_vol, audio_vol, output_length, job_id, webhook_url=None): + video_path = download_file(video_url, STORAGE_PATH) audio_path = download_file(audio_url, STORAGE_PATH) output_path = os.path.join(STORAGE_PATH, f"{job_id}.mp4") - # Apply volume filters and mix audio + + # Prepare FFmpeg inputs video = ffmpeg.input(video_path) audio = ffmpeg.input(audio_path) - video_audio = video.audio.filter('volume', volume=video_vol / 100.0) - input_audio = audio.filter('volume', volume=audio_vol / 100.0) - mixed_audio = ffmpeg.filter([video_audio, input_audio], 'amix', duration='longest') - if output_length == 'audio': - output = ffmpeg.output(video.video, mixed_audio, output_path, shortest=None) + # Apply volume filters + video_audio = video.audio.filter('volume', volume=f"{video_vol/100}") + input_audio = audio.filter('volume', volume=f"{audio_vol/100}") + + # Get video and audio durations + video_info = ffmpeg.probe(video_path) + audio_info = ffmpeg.probe(audio_path) + video_duration = float(video_info['streams'][0]['duration']) + audio_duration = float(audio_info['streams'][0]['duration']) + + # Determine output duration + if output_length == 'video': + output_duration = video_duration else: - output = ffmpeg.output(video.video, mixed_audio, output_path, shortest=None) + output_duration = audio_duration + + # Trim or pad audio + if output_duration > audio_duration: + input_audio = ffmpeg.filter([input_audio], 'apad', pad_dur=output_duration-audio_duration) + else: + input_audio = input_audio.filter('atrim', duration=output_duration) + + # Mix audio + mixed_audio = ffmpeg.filter([video_audio, input_audio], 'amix', inputs=2) + + # Trim or pad video + if output_duration > video_duration: + video = ffmpeg.filter([video], 'tpad', stop_duration=output_duration-video_duration) + else: + video = video.trim(duration=output_duration) + + # Combine video and mixed audio + output = ffmpeg.output(video, mixed_audio, output_path, + vcodec='libx264', acodec='aac', strict='experimental') + + # Run FFmpeg command + ffmpeg.run(output, overwrite_output=True) + + # Clean up input files + os.remove(video_path) + os.remove(audio_path) - ffmpeg.run(output) - return output_path + return output_path \ No newline at end of file