From ac688e990b757a269bd90014945218dc6f66d85c Mon Sep 17 00:00:00 2001 From: Stephen G Pope Date: Wed, 18 Sep 2024 17:27:59 -0700 Subject: [PATCH] changed input variable names and allow the download of SRT file --- routes/caption_video.py | 10 +++++----- services/caption_video.py | 20 +++++++++++++++++--- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/routes/caption_video.py b/routes/caption_video.py index 6857ae0..fa71dad 100644 --- a/routes/caption_video.py +++ b/routes/caption_video.py @@ -14,8 +14,8 @@ def process_job(data, job_id): try: logger.info(f"Job {job_id}: Starting captioning process") - file_url = data['file_url'] - caption_srt = data['caption_srt'] + file_url = data['video_url'] + caption_srt = data['srt'] options = data.get('options', {}) output_filename = process_captioning(file_url, caption_srt, options, job_id) @@ -56,9 +56,9 @@ def caption_video(): logger.error("Missing X-API-Key header") return jsonify({"message": "Missing X-API-Key header"}), 400 - if not all(k in data for k in ('file_url', 'caption_srt')): - logger.error("file_url and caption_srt are required") - return jsonify({"message": "file_url and caption_srt are required"}), 400 + if not all(k in data for k in ('video_url', 'srt')): + logger.error("video_url and srt are required") + return jsonify({"message": "video_url and srt are required"}), 400 job_id = str(uuid.uuid4()) logger.info(f"Processing Job ID: {job_id}") diff --git a/services/caption_video.py b/services/caption_video.py index e0a91b7..6bcbbac 100644 --- a/services/caption_video.py +++ b/services/caption_video.py @@ -45,8 +45,22 @@ def process_captioning(file_url, caption_srt, options, job_id): logger.info(f"Job {job_id}: File downloaded to {video_path}") srt_path = os.path.join(STORAGE_PATH, f"{job_id}.srt") - with open(srt_path, 'w') as srt_file: - srt_file.write(caption_srt) + + if caption_srt.startswith("https"): + # Download the file if caption_srt is a URL + logger.info(f"Job {job_id}: Downloading caption file from {caption_srt}") + response = requests.get(caption_srt) + response.raise_for_status() # Raise an exception for bad status codes + + with open(srt_path, 'wb') as srt_file: + srt_file.write(response.content) + + logger.info(f"Job {job_id}: Caption file downloaded to {srt_path}") + else: + # Write caption_srt content directly to file + with open(srt_path, 'w') as srt_file: + srt_file.write(caption_srt) + logger.info(f"Job {job_id}: SRT file created at {srt_path}") output_path = os.path.join(STORAGE_PATH, f"{job_id}_captioned.mp4") @@ -120,7 +134,7 @@ def process_captioning(file_url, caption_srt, options, job_id): output_path, vf=subtitle_filter, acodec='copy', - ).run(capture_stdout=True, capture_stderr=True) + ).run() logger.info(f"Job {job_id}: FFmpeg processing completed, output file at {output_path}") except ffmpeg.Error as e: # Log the FFmpeg stderr output