Update video_script.py
Browse files- video_script.py +5 -30
video_script.py
CHANGED
|
@@ -10,7 +10,6 @@ import subprocess
|
|
| 10 |
from urllib.request import urlretrieve
|
| 11 |
from openai import OpenAI
|
| 12 |
from dotenv import load_dotenv
|
| 13 |
-
import langdetect
|
| 14 |
|
| 15 |
# Load environment variables
|
| 16 |
load_dotenv()
|
|
@@ -158,23 +157,6 @@ def split_into_chunks(text, chunk_size=3):
|
|
| 158 |
words = text.split()
|
| 159 |
return [' '.join(words[i:i+chunk_size]) for i in range(0, len(words), chunk_size)]
|
| 160 |
|
| 161 |
-
def detect_language(text):
|
| 162 |
-
try:
|
| 163 |
-
return langdetect.detect(text)
|
| 164 |
-
except:
|
| 165 |
-
return 'en' # Default to English if detection fails
|
| 166 |
-
|
| 167 |
-
def get_font_for_language(lang):
|
| 168 |
-
# Map of language codes to appropriate fonts
|
| 169 |
-
lang_font_map = {
|
| 170 |
-
'en': 'Impact', # English
|
| 171 |
-
'hi': 'Noto Sans Devanagari', # Hindi
|
| 172 |
-
'ur': 'Noto Sans Arabic', # Urdu
|
| 173 |
-
'ar': 'Noto Sans Arabic', # Arabic
|
| 174 |
-
# Add more mappings as needed
|
| 175 |
-
}
|
| 176 |
-
return lang_font_map.get(lang, 'Arial Unicode MS') # Default to a Unicode-compatible font
|
| 177 |
-
|
| 178 |
def create_ass_file(chunks, chunk_duration, output_ass, video_width, video_height):
|
| 179 |
font_size = int(video_height * 0.05)
|
| 180 |
impact_font_url = "https://picfy.xyz/uploads/impact.ttf"
|
|
@@ -184,25 +166,18 @@ def create_ass_file(chunks, chunk_duration, output_ass, video_width, video_heigh
|
|
| 184 |
if not os.path.exists(impact_font_path):
|
| 185 |
urlretrieve(impact_font_url, impact_font_path)
|
| 186 |
|
| 187 |
-
with open(output_ass, 'w'
|
| 188 |
f.write("[Script Info]\nScriptType: v4.00+\nPlayResX: {}\nPlayResY: {}\n\n".format(video_width, video_height))
|
| 189 |
f.write("[V4+ Styles]\nFormat: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding\n")
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
for lang in set(detect_language(chunk) for chunk in chunks):
|
| 193 |
-
font = get_font_for_language(lang)
|
| 194 |
-
f.write(f"Style: {lang},\{font},{font_size},&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,0,5,10,10,10,1\n")
|
| 195 |
-
|
| 196 |
-
f.write("\n[Events]\nFormat: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n")
|
| 197 |
|
| 198 |
for i, chunk in enumerate(chunks):
|
| 199 |
start_time = i * chunk_duration
|
| 200 |
end_time = (i + 1) * chunk_duration
|
| 201 |
-
|
| 202 |
-
f.write("Dialogue: 0,{},{},{},0,0,0,,{}\n".format(
|
| 203 |
format_time(start_time),
|
| 204 |
format_time(end_time),
|
| 205 |
-
lang,
|
| 206 |
chunk
|
| 207 |
))
|
| 208 |
|
|
@@ -225,7 +200,7 @@ def add_captions_to_video(video_file, audio_file, output_file):
|
|
| 225 |
ass_file = "subtitles.ass"
|
| 226 |
create_ass_file(chunks, chunk_duration, ass_file, int(video_width), int(video_height))
|
| 227 |
|
| 228 |
-
ffmpeg_cmd = f"ffmpeg -i {video_file} -i {audio_file} -vf \"
|
| 229 |
subprocess.run(ffmpeg_cmd, shell=True, check=True)
|
| 230 |
|
| 231 |
os.remove(ass_file)
|
|
|
|
| 10 |
from urllib.request import urlretrieve
|
| 11 |
from openai import OpenAI
|
| 12 |
from dotenv import load_dotenv
|
|
|
|
| 13 |
|
| 14 |
# Load environment variables
|
| 15 |
load_dotenv()
|
|
|
|
| 157 |
words = text.split()
|
| 158 |
return [' '.join(words[i:i+chunk_size]) for i in range(0, len(words), chunk_size)]
|
| 159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
def create_ass_file(chunks, chunk_duration, output_ass, video_width, video_height):
|
| 161 |
font_size = int(video_height * 0.05)
|
| 162 |
impact_font_url = "https://picfy.xyz/uploads/impact.ttf"
|
|
|
|
| 166 |
if not os.path.exists(impact_font_path):
|
| 167 |
urlretrieve(impact_font_url, impact_font_path)
|
| 168 |
|
| 169 |
+
with open(output_ass, 'w') as f:
|
| 170 |
f.write("[Script Info]\nScriptType: v4.00+\nPlayResX: {}\nPlayResY: {}\n\n".format(video_width, video_height))
|
| 171 |
f.write("[V4+ Styles]\nFormat: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding\n")
|
| 172 |
+
f.write("Style: Default,Impact,{},&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,0,5,10,10,10,1\n\n".format(font_size))
|
| 173 |
+
f.write("[Events]\nFormat: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
for i, chunk in enumerate(chunks):
|
| 176 |
start_time = i * chunk_duration
|
| 177 |
end_time = (i + 1) * chunk_duration
|
| 178 |
+
f.write("Dialogue: 0,{},{},Default,,0,0,0,,{}\n".format(
|
|
|
|
| 179 |
format_time(start_time),
|
| 180 |
format_time(end_time),
|
|
|
|
| 181 |
chunk
|
| 182 |
))
|
| 183 |
|
|
|
|
| 200 |
ass_file = "subtitles.ass"
|
| 201 |
create_ass_file(chunks, chunk_duration, ass_file, int(video_width), int(video_height))
|
| 202 |
|
| 203 |
+
ffmpeg_cmd = f"ffmpeg -i {video_file} -i {audio_file} -vf \"ass={ass_file}:fontsdir=.\" -c:a aac -c:v libx264 {output_file}"
|
| 204 |
subprocess.run(ffmpeg_cmd, shell=True, check=True)
|
| 205 |
|
| 206 |
os.remove(ass_file)
|