New audio to video conversion tool is ready

This commit is contained in:
Scott Idem
2023-11-10 17:29:58 -05:00
parent 1cfd2421bd
commit 243c07101e

View File

@@ -1261,30 +1261,31 @@ async def create_video(
title_part_1: str = Form(...), title_part_1: str = Form(...),
title_part_2: str = Form(...), title_part_2: str = Form(...),
subtitle_part_1: str = Form(...), subtitle_part_1: str = Form(...),
subtitle_part_2: str = Form(...) subtitle_part_2: str = Form(...),
font_color: str = Form('darkblue'),
): ):
log.setLevel(logging.DEBUG) log.setLevel(logging.DEBUG)
log.debug(locals()) log.debug(locals())
# cmd = f"rm /tmp/*.jpg" # # cmd = f"rm /tmp/*.jpg"
cmd = f"ffmpeg -version" # cmd = f"ffmpeg -version"
args = shlex.split(cmd) # args = shlex.split(cmd)
try: # try:
result = subprocess.run(args, check=True, capture_output=True, text=True) # result = subprocess.run(args, check=True, capture_output=True, text=True)
log.debug(result.stdout) # log.debug(result.stdout)
except subprocess.CalledProcessError as e: # except subprocess.CalledProcessError as e:
log.exception('Error running test command') # log.exception('Error running test command')
return {'success': False, 'status_message': f'Error running test command: {e}'} # return {'success': False, 'status_message': f'Error running test command: {e}'}
# cmd = f"convert --version" # # cmd = f"convert --version"
cmd = f"convert -list font" # cmd = f"convert -list font"
args = shlex.split(cmd) # args = shlex.split(cmd)
try: # try:
result = subprocess.run(args, check=True, capture_output=True, text=True) # result = subprocess.run(args, check=True, capture_output=True, text=True)
log.debug(result.stdout) # log.debug(result.stdout)
except subprocess.CalledProcessError as e: # except subprocess.CalledProcessError as e:
log.exception('Error running ImageMagick convert command') # log.exception('Error running ImageMagick convert command')
return {'success': False, 'status_message': f'Error running ImageMagick convert command: {e}'} # return {'success': False, 'status_message': f'Error running ImageMagick convert command: {e}'}
# Save the uploaded audio file to a temporary directory # Save the uploaded audio file to a temporary directory
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as audio_file: with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as audio_file:
@@ -1306,8 +1307,8 @@ async def create_video(
with Drawing() as ctx: with Drawing() as ctx:
ctx.font_family = 'DejaVu Sans' # DejaVu Sans, DejaVu Sans Mono, DejaVu Serif ctx.font_family = 'DejaVu Sans' # DejaVu Sans, DejaVu Sans Mono, DejaVu Serif
ctx.font_size = 72 ctx.font_size = 72
ctx.fill_color = 'darkblue' ctx.fill_color = font_color
ctx.stroke_color = 'darkblue' ctx.stroke_color = font_color
ctx.gravity = 'north' ctx.gravity = 'north'
ctx.text_decoration = 'underline' ctx.text_decoration = 'underline'
@@ -1320,71 +1321,9 @@ async def create_video(
ctx.text(0, 0, f'{subtitle_part_1}') ctx.text(0, 0, f'{subtitle_part_1}')
ctx.text(0, 62, f'{subtitle_part_2}') ctx.text(0, 62, f'{subtitle_part_2}')
# ctx.text(30, 25, 'TOP')
# ctx.text(30, 200, 'BOTTOM')
# ctx.push()
ctx(img) ctx(img)
img.save(filename=title_image_path) 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)
# cmd = f"convert -size 1280x720 xc:transparent -gravity center -pointsize 72 -fill green -annotate 0 '{presentation_name}\n{speaker_name}' {image_name}"
# cmd = f"convert -size 1280x720 xc:transparent -annotate 0 'Hello World' {image_name}"
# cmd = f"convert -size 1280x720 -background lightblue -fill darkblue -pointsize 72 label:{presentation_name} {image_name}"
# cmd = f"convert -size 1280x720 -background lightblue -fill darkblue label:'Testing' {image_name}"
# cmd = f"convert -size 1280x720 {image_name}"
# args = shlex.split(cmd)
# try:
# subprocess.run(args, check=True)
# except subprocess.CalledProcessError as e:
# log.exception('Error running convert command')
# return {'success': False, 'status_message': f'Error running convert command: {e}'}
cmd = f"ls -lha /tmp" cmd = f"ls -lha /tmp"
args = shlex.split(cmd) args = shlex.split(cmd)
@@ -1401,35 +1340,17 @@ async def create_video(
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as video_file: with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as video_file:
video_name = video_file.name 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. # 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} &" # NOTE: This is a blocking process. It will take a while to complete.
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 -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 -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 -i {audio_file_path} -c:a aac {video_name}" # cmd = f"ffmpeg -hide_banner -i {audio_file_path} -c:a aac {video_name}"
log.debug(cmd) log.debug(cmd)
# Run the ffmpeg command # Run the ffmpeg command
# process = await run_ffmpeg(cmd)
# # Monitor the status of the ffmpeg command
# while process.poll() is None:
# # Read the ffmpeg output
# stdout, stderr = process.communicate(timeout=1)
# # Log the ffmpeg output
# logging.debug(stdout)
# logging.debug(stderr)
# # Wait for the ffmpeg command to finish
# process.wait()
args = shlex.split(cmd) args = shlex.split(cmd)
try: try:
subprocess.run(args, check=True, capture_output=True, text=True) subprocess.run(args, check=True, capture_output=True, text=True)
log.debug(result.stdout) log.debug(result.stdout)
# subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# log.debug(result.stdout)
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
log.exception('Error running ffmpeg command') log.exception('Error running ffmpeg command')
return {'success': False, 'status_message': f'Error running ffmpeg command: {e}'} return {'success': False, 'status_message': f'Error running ffmpeg command: {e}'}