Skip to content

Commit

Permalink
more clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen G Pope authored and Stephen G Pope committed Sep 13, 2024
1 parent ba40192 commit 6767d3b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 19 deletions.
15 changes: 12 additions & 3 deletions routes/audio_mixing.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,31 @@ 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:
current_app.logger.error(f"Job {job_id}: Error during processing - {e}")
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
return jsonify(
{
"code": 202,
"id": data.get("id"),
"job_id": job_id,
"message": "processing"
}
), 202
16 changes: 9 additions & 7 deletions routes/transcribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand All @@ -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:
Expand Down Expand Up @@ -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,
Expand Down
53 changes: 44 additions & 9 deletions services/audio_mixing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 6767d3b

Please sign in to comment.