mirror of
https://github.com/denizsafak/abogen.git
synced 2026-07-18 13:40:27 +02:00
Corrections to code logic
This commit is contained in:
+43
-28
@@ -2,7 +2,7 @@
|
||||
#
|
||||
# For WebM (smaller filesize):
|
||||
# ffmpeg -loop 1 -framerate 24 -i bg.jpg -i audio.wav -vf "ass=your_subtitle_demo.ass" -c:v libvpx-vp9 -b:v 0 -crf 30 -c:a libopus -shortest demo.webm
|
||||
#
|
||||
#
|
||||
# For MP4 (higher quality):
|
||||
# ffmpeg -loop 1 -framerate 24 -i bg.jpg -i audio.wav -vf "ass=your_subtitle_demo.ass" -c:v libx264 -preset slow -crf 18 -movflags +faststart -c:a copy -shortest demo.mp4
|
||||
#
|
||||
@@ -13,23 +13,27 @@ import re
|
||||
import sys
|
||||
from datetime import timedelta
|
||||
|
||||
|
||||
def parse_time(s):
|
||||
# For ASS format
|
||||
if ':' in s and '.' in s:
|
||||
h, m, s = s.split(':')
|
||||
sec, cs = s.split('.')
|
||||
return timedelta(hours=int(h), minutes=int(m), seconds=int(sec), milliseconds=int(cs) * 10)
|
||||
if ":" in s and "." in s:
|
||||
h, m, s = s.split(":")
|
||||
sec, cs = s.split(".")
|
||||
return timedelta(
|
||||
hours=int(h), minutes=int(m), seconds=int(sec), milliseconds=int(cs) * 10
|
||||
)
|
||||
# For SRT format (00:00:00,000)
|
||||
elif ':' in s and ',' in s:
|
||||
parts = s.split(':')
|
||||
elif ":" in s and "," in s:
|
||||
parts = s.split(":")
|
||||
h = int(parts[0])
|
||||
m = int(parts[1])
|
||||
sec_parts = parts[2].split(',')
|
||||
sec_parts = parts[2].split(",")
|
||||
sec = int(sec_parts[0])
|
||||
ms = int(sec_parts[1])
|
||||
return timedelta(hours=h, minutes=m, seconds=sec, milliseconds=ms)
|
||||
return None
|
||||
|
||||
|
||||
def format_time(t):
|
||||
total_seconds = int(t.total_seconds())
|
||||
cs = int((t.total_seconds() - total_seconds) * 100)
|
||||
@@ -38,6 +42,7 @@ def format_time(t):
|
||||
s = total_seconds % 60
|
||||
return f"{h}:{m:02}:{s:02}.{cs:02}"
|
||||
|
||||
|
||||
# Desired script info and style
|
||||
DESIRED_SCRIPT_INFO = """[Script Info]
|
||||
Title: Centered Subs
|
||||
@@ -51,8 +56,9 @@ Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour,
|
||||
Style: Default,Arial,60,&H00FFFFFF,&H000000FF,&H00000000,&H64000000,-1,0,0,0,100,100,0,0,1,2,0,5,10,10,10,1
|
||||
"""
|
||||
|
||||
|
||||
def process_subtitle_file(input_file):
|
||||
output_file = input_file.replace('.ass', '_demo.ass').replace('.srt', '_demo.ass')
|
||||
output_file = input_file.replace(".ass", "_demo.ass").replace(".srt", "_demo.ass")
|
||||
if output_file == input_file:
|
||||
output_file = f"{input_file}_demo.ass"
|
||||
|
||||
@@ -67,11 +73,11 @@ def process_subtitle_file(input_file):
|
||||
sys.exit(1)
|
||||
|
||||
# Check if it's an SRT file
|
||||
is_srt = input_file.lower().endswith('.srt')
|
||||
|
||||
is_srt = input_file.lower().endswith(".srt")
|
||||
|
||||
if is_srt:
|
||||
return convert_srt_to_ass(input_file, output_file, lines)
|
||||
|
||||
|
||||
# Process ASS file
|
||||
header = []
|
||||
events = []
|
||||
@@ -124,9 +130,12 @@ def process_subtitle_file(input_file):
|
||||
if end > next_start:
|
||||
end = next_start # cut current subtitle to stop at next one's start
|
||||
|
||||
fixed_line = re.sub(r"Dialogue: \d,[^,]+,[^,]+,",
|
||||
f"Dialogue: 0,{format_time(start)},{format_time(end)},",
|
||||
line, count=1)
|
||||
fixed_line = re.sub(
|
||||
r"Dialogue: \d,[^,]+,[^,]+,",
|
||||
f"Dialogue: 0,{format_time(start)},{format_time(end)},",
|
||||
line,
|
||||
count=1,
|
||||
)
|
||||
fixed_lines.append(fixed_line)
|
||||
|
||||
try:
|
||||
@@ -138,12 +147,13 @@ def process_subtitle_file(input_file):
|
||||
print(f"❌ Error writing output file: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def convert_srt_to_ass(input_file, output_file, lines):
|
||||
"""Convert SRT format to ASS format."""
|
||||
events = []
|
||||
subtitle_blocks = []
|
||||
current_block = []
|
||||
|
||||
|
||||
# Parse SRT file
|
||||
for line in lines:
|
||||
line = line.strip()
|
||||
@@ -153,41 +163,45 @@ def convert_srt_to_ass(input_file, output_file, lines):
|
||||
current_block = []
|
||||
else:
|
||||
current_block.append(line)
|
||||
|
||||
|
||||
# Don't forget the last block
|
||||
if current_block:
|
||||
subtitle_blocks.append(current_block)
|
||||
|
||||
|
||||
# Process subtitle blocks
|
||||
for block in subtitle_blocks:
|
||||
if len(block) < 3:
|
||||
continue # Invalid block
|
||||
|
||||
|
||||
# Skip the subtitle number
|
||||
timing_line = block[1]
|
||||
|
||||
|
||||
# Parse timing information
|
||||
timing_match = re.match(r'(\d+:\d+:\d+,\d+)\s+-->\s+(\d+:\d+:\d+,\d+)', timing_line)
|
||||
timing_match = re.match(
|
||||
r"(\d+:\d+:\d+,\d+)\s+-->\s+(\d+:\d+:\d+,\d+)", timing_line
|
||||
)
|
||||
if not timing_match:
|
||||
continue
|
||||
|
||||
|
||||
start_time = parse_time(timing_match.group(1))
|
||||
end_time = parse_time(timing_match.group(2))
|
||||
|
||||
|
||||
# Combine text lines
|
||||
text = "\\N".join(block[2:])
|
||||
|
||||
|
||||
# Create ASS Dialogue line
|
||||
dialogue_line = f"Dialogue: 0,{format_time(start_time)},{format_time(end_time)},Default,,0,0,0,,{text}\n"
|
||||
events.append(dialogue_line)
|
||||
|
||||
|
||||
# Create ASS file
|
||||
try:
|
||||
with open(output_file, "w", encoding="utf-8") as f:
|
||||
f.write(DESIRED_SCRIPT_INFO)
|
||||
f.write(DESIRED_STYLE)
|
||||
f.write("[Events]\n")
|
||||
f.write("Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n")
|
||||
f.write(
|
||||
"Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n"
|
||||
)
|
||||
f.writelines(events)
|
||||
print(f"✅ Successfully converted SRT to ASS. Output file: {output_file}")
|
||||
return True
|
||||
@@ -195,11 +209,12 @@ def convert_srt_to_ass(input_file, output_file, lines):
|
||||
print(f"❌ Error writing output file: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) < 2:
|
||||
print("❌ Error: No input file specified")
|
||||
print(f"Usage: {sys.argv[0]} <input_file.ass|input_file.srt>")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
input_file = sys.argv[1]
|
||||
process_subtitle_file(input_file)
|
||||
process_subtitle_file(input_file)
|
||||
|
||||
Reference in New Issue
Block a user