Skip to content

Commit

Permalink
changed input variable names and allow the download of SRT file
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen G Pope authored and Stephen G Pope committed Sep 19, 2024
1 parent 9d6a734 commit ac688e9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
10 changes: 5 additions & 5 deletions routes/caption_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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}")
Expand Down
20 changes: 17 additions & 3 deletions services/caption_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ac688e9

Please sign in to comment.