Spaces:
Sleeping
Sleeping
File size: 4,841 Bytes
aff0a09 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
import subprocess
from utils.video_to_audio import extract_audio
from utils.transcribe_audio import transcribe_audio
from utils.create_subtitles import create_subtitles, hex_to_ffmpeg_color
import os
def generate_subway_subtitles(customization_options):
customization_options['font_color'] = hex_to_ffmpeg_color(customization_options['font_color'])
audio_file = "./data/subway/audio.mp3"
subtitle_file = "./data/subway/subtitles.srt"
top_video_path = customization_options['top_video']
bottom_video_path = customization_options['bottom_video']
output_directory = "./data/subway/videos"
extract_audio(bottom_video_path, audio_file)
transcription = transcribe_audio(audio_file)
create_subtitles(transcription, subtitle_file)
trimmed_output_filepath = top_video_operations(top_video_path, bottom_video_path, output_directory)
bottom_video_width, bottom_video_height = bottom_video_operations(bottom_video_path)
combined_output_path = combination_video(trimmed_output_filepath, bottom_video_path, bottom_video_width, bottom_video_height, output_directory)
subtitle_output_filepath = add_subtitle(combined_output_path, subtitle_file, customization_options, output_directory)
result_video_path = add_text_to_video(subtitle_output_filepath, customization_options, output_directory)
return result_video_path
def top_video_operations(top_video_path, bottom_video_path, output_directory):
muted_output_filepath = os.path.join(output_directory, 'top_video_muted.mp4')
trimmed_output_filepath = os.path.join(output_directory, 'top_video_muted_trimmed.mp4')
mute_top_cmd = [
"ffmpeg",
"-i", top_video_path,
"-af", "volume=0.0",
"-c:v", "copy",
"-y", muted_output_filepath
]
subprocess.run(mute_top_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
get_duration_cmd = [
"ffprobe",
"-v", "error",
"-show_entries", "format=duration",
"-of", "default=noprint_wrappers=1:nokey=1",
bottom_video_path
]
duration = float(subprocess.check_output(get_duration_cmd, universal_newlines=True).strip())
trim_top_cmd = [
"ffmpeg",
"-i", muted_output_filepath,
"-t", str(duration),
"-c:v", "copy",
"-y", trimmed_output_filepath
]
subprocess.run(trim_top_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return trimmed_output_filepath
def bottom_video_operations(bottom_video_path):
bottom_video_height = int(subprocess.check_output([
"ffprobe",
"-v", "error",
"-select_streams", "v:0",
"-show_entries", "stream=height",
"-of", "csv=s=x:p=0",
bottom_video_path
], universal_newlines=True).strip())
bottom_video_width = int(subprocess.check_output([
"ffprobe",
"-v", "error",
"-select_streams", "v:0",
"-show_entries", "stream=width",
"-of", "csv=s=x:p=0",
bottom_video_path
], universal_newlines=True).strip())
return bottom_video_width, bottom_video_height
def combination_video(trimmed_top_video_path, bottom_video_path, bottom_video_width, bottom_video_height, output_directory):
combined_output_filepath = os.path.join(output_directory, 'top_video_muted_trimmed_combined.mp4')
combine_cmd = [
"ffmpeg",
"-i", trimmed_top_video_path,
"-i", bottom_video_path,
"-filter_complex", f"[0:v]scale={bottom_video_width}:{bottom_video_height},setsar=1[main];[1:v]scale={bottom_video_width}:{bottom_video_height},setsar=1[bottom_scaled];[main][bottom_scaled]vstack=inputs=2[v];[0:a][1:a]amix=inputs=2[a]",
"-map", "[v]",
"-map", "[a]",
"-y", combined_output_filepath
]
subprocess.run(combine_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return combined_output_filepath
def add_subtitle(combined_video_path, subtitle_file, customization_options, output_directory):
subtitle_output_filepath = os.path.join(output_directory, 'combined_subtitle.mp4')
subtitle_cmd = [
"ffmpeg",
"-i", combined_video_path,
"-vf", f"subtitles={subtitle_file}:force_style='Fontsize={customization_options['font_size']},Fontname={customization_options['font_family']},BorderColor=black@{customization_options['border_size']},PrimaryColour={customization_options['font_color']}'",
"-c:a", "aac",
"-c:v", "libx264",
"-y", subtitle_output_filepath,
]
subprocess.run(subtitle_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return subtitle_output_filepath
def add_text_to_video(subtitle_output_filepath, customization_options, output_directory):
result_video_path = os.path.join(output_directory, 'combined_subtitle_text.mp4')
cmd = [
'ffmpeg',
'-i', subtitle_output_filepath,
'-vf', f'drawtext=text=\'{customization_options["credit"]}\':fontcolor=white:fontsize={customization_options["credit_size"]}:box=1:[email protected]:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2',
'-codec:a', 'copy',
"-y", result_video_path
]
subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return result_video_path
|