Finalizing the new audio to video file converter

This commit is contained in:
Scott Idem
2023-11-10 15:54:04 -05:00
parent be6fc9ffb5
commit 1cfd2421bd

View File

@@ -1257,8 +1257,11 @@ def run_ffmpeg(cmd):
@router.post('/create_video')
async def create_video(
file: UploadFile = File(...),
presentation_name: str = Form(...),
speaker_name: str = Form(...)
title_image: UploadFile = File(None),
title_part_1: str = Form(...),
title_part_2: str = Form(...),
subtitle_part_1: str = Form(...),
subtitle_part_2: str = Form(...)
):
log.setLevel(logging.DEBUG)
log.debug(locals())
@@ -1288,25 +1291,86 @@ async def create_video(
audio_file.write(await file.read())
audio_file_path = audio_file.name
# Generate a static image using the presentation name and speaker name
with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as image_file:
image_name = image_file.name
log.debug(image_name)
# Save the uploaded title image file to a temporary directory
if title_image:
with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as title_image_opened:
title_image_opened.write(await title_image.read())
title_image_path = title_image_opened.name
else:
with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as title_image_opened:
title_image_path = title_image_opened.name
with Image(width=1280, height=720, background='lightblue') as img:
img.save(filename=title_image_path)
with Image(width=1280, height=720, background='transparent') as img:
with Drawing() as ctx:
ctx.font_family = 'DejaVu Sans' # DejaVu Sans, DejaVu Sans Mono, DejaVu Serif
# ctx.font = 'Times New Roman' # DejaVu-Sans, DejaVu-Sans-Bold, DejaVu-Sans-Mono, DejaVu-Sans-Mono-Bold, DejaVu-Serif
ctx.font_size = 64
ctx.text_alignment = 'center'
ctx.text_decoration = 'underline'
ctx.text_kerning = -1
ctx.fill_color = 'darkblue'
ctx.stroke_color = 'gray'
img.annotate(f'{presentation_name}\n{speaker_name}', ctx, left=50, baseline=0, angle=0)
# gravity='center'
# , font_size=72, fill='green'
img.save(filename=image_name)
with Image(filename=title_image_path) as img:
with Drawing() as ctx:
ctx.font_family = 'DejaVu Sans' # DejaVu Sans, DejaVu Sans Mono, DejaVu Serif
ctx.font_size = 72
ctx.fill_color = 'darkblue'
ctx.stroke_color = 'darkblue'
ctx.gravity = 'north'
ctx.text_decoration = 'underline'
ctx.text(0, 150, f'{title_part_1}')
ctx.text(0, 222, f'{title_part_2}')
ctx.gravity = 'center'
ctx.font_size = 62
ctx.text_decoration = 'no'
ctx.text(0, 0, f'{subtitle_part_1}')
ctx.text(0, 62, f'{subtitle_part_2}')
# ctx.text(30, 25, 'TOP')
# ctx.text(30, 200, 'BOTTOM')
# ctx.push()
ctx(img)
img.save(filename=title_image_path)
# # Open saved title image and add the title and subtitle text
# with tempfile.NamedTemporaryFile() as image_file:
# image_name = image_file.name
# log.debug(image_name)
# with Image(filename=image_name) as img:
# with Drawing() as ctx:
# ctx.font_family = 'DejaVu Sans' # DejaVu Sans, DejaVu Sans Mono, DejaVu Serif
# ctx.font_size = 72
# ctx.fill_color = 'darkblue'
# ctx.stroke_color = 'darkblue'
# ctx.gravity = 'north'
# ctx.text_decoration = 'underline'
# ctx.text(0, 150, f'{title_part_1}')
# ctx.text(0, 222, f'{title_part_2}')
# ctx.gravity = 'center'
# ctx.font_size = 64
# ctx.text_decoration = 'no'
# ctx.text(0, 0, f'{subtitle_part_1}')
# ctx.text(0, 64, f'{subtitle_part_2}')
# # ctx.text(30, 25, 'TOP')
# # ctx.text(30, 200, 'BOTTOM')
# # ctx.push()
# ctx(img)
# # with Image(width=1280, height=720, background='transparent') as img:
# with Image(filename=image_name) as img:
# with Drawing() as ctx:
# ctx.font_family = 'DejaVu Sans' # DejaVu Sans, DejaVu Sans Mono, DejaVu Serif
# # ctx.font = 'Times New Roman' # DejaVu-Sans, DejaVu-Sans-Bold, DejaVu-Sans-Mono, DejaVu-Sans-Mono-Bold, DejaVu-Serif
# ctx.font_size = 64
# ctx.text_alignment = 'center'
# ctx.text_decoration = 'underline'
# ctx.text_kerning = -1
# ctx.fill_color = 'darkblue'
# ctx.stroke_color = 'gray'
# img.annotate(f'{presentation_name}\n{speaker_name}', ctx, left=50, baseline=0, angle=0)
# # gravity='center'
# # , font_size=72, fill='green'
# img.save(filename=image_name)
@@ -1338,7 +1402,7 @@ async def create_video(
video_name = video_file.name
# NOTE: It seems very important that the -y argument is used with ffmpeg run by subprocess.run(). Otherwise the process will hang.
# cmd = f"ffmpeg -nostdin -loglevel error -loop 1 -i {image_name} -i {audio_file_path} -c:a copy -c:v libx264 -shortest {video_name} &"
cmd = f"ffmpeg -hide_banner -loglevel error -nostats -y -loop 1 -i {image_name} -i {audio_file_path} -c:a copy -c:v libx264 -shortest {video_name}"
cmd = f"ffmpeg -hide_banner -loglevel error -nostats -y -loop 1 -i {title_image_path} -i {audio_file_path} -c:a copy -c:v libx264 -shortest {video_name}"
# cmd = f"ffmpeg -hide_banner -i {audio_file_path} -c:a aac {video_name}"
log.debug(cmd)
@@ -1371,4 +1435,4 @@ async def create_video(
return {'success': False, 'status_message': f'Error running ffmpeg command: {e}'}
# Return the new mp4 file for download
return FileResponse(video_name, media_type='video/mp4', filename=f'{presentation_name}_{speaker_name}.mp4')
return FileResponse(video_name, media_type='video/mp4', filename=f'{title_part_1}_{subtitle_part_1}.mp4')