improve example subrip to mp3 perf
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
@@ -14,7 +15,7 @@ if shutil.which("ffmpeg") is None:
|
|||||||
|
|
||||||
|
|
||||||
def parse_srt(srt_file):
|
def parse_srt(srt_file):
|
||||||
with open(srt_file, "r") as f:
|
with open(srt_file, "r", encoding="utf-8") as f:
|
||||||
data = f.read().strip().split("\n\n")
|
data = f.read().strip().split("\n\n")
|
||||||
data = [i.strip() for i in data]
|
data = [i.strip() for i in data]
|
||||||
data = [(*i.split("\n")[:2], " ".join(i.split("\n")[2:])) for i in data]
|
data = [(*i.split("\n")[:2], " ".join(i.split("\n")[2:])) for i in data]
|
||||||
@@ -86,55 +87,79 @@ async def _main(srt_data, voice_name, out_file):
|
|||||||
stderr=subprocess.DEVNULL,
|
stderr=subprocess.DEVNULL,
|
||||||
)
|
)
|
||||||
|
|
||||||
for i in srt_data:
|
input_files = []
|
||||||
duration = i[1].replace(",", ".")
|
input_files_start = {}
|
||||||
duration = duration.split("-->")
|
|
||||||
|
|
||||||
start = duration[0].split(":")
|
with tempfile.TemporaryDirectory() as temp_dir:
|
||||||
start = int(start[0]) * 3600 + int(start[1]) * 60 + float(start[2])
|
for i in srt_data:
|
||||||
|
print(f"Processing {i[0]}...")
|
||||||
|
|
||||||
end = duration[1].split(":")
|
fname = os.path.join(temp_dir, f"{i[0]}.mp3")
|
||||||
end = int(end[0]) * 3600 + int(end[1]) * 60 + float(end[2])
|
input_files.append(fname)
|
||||||
|
|
||||||
duration = end - start
|
duration = i[1].replace(",", ".")
|
||||||
with tempfile.NamedTemporaryFile(suffix=".mp3") as temporary_file:
|
duration = duration.split("-->")
|
||||||
async for j in communicate.run(
|
|
||||||
i[2], codec="audio-24khz-48kbitrate-mono-mp3", voice=voice_name
|
|
||||||
):
|
|
||||||
if j[2] is not None:
|
|
||||||
temporary_file.write(j[2])
|
|
||||||
|
|
||||||
temporary_file2 = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
|
start = duration[0].split(":")
|
||||||
try:
|
start = int(start[0]) * 3600 + int(start[1]) * 60 + float(start[2])
|
||||||
ensure_audio_length(
|
input_files_start[fname] = start
|
||||||
temporary_file.name, temporary_file2.name, duration
|
|
||||||
|
end = duration[1].split(":")
|
||||||
|
end = int(end[0]) * 3600 + int(end[1]) * 60 + float(end[2])
|
||||||
|
|
||||||
|
duration = end - start
|
||||||
|
with open(fname, "wb") as f:
|
||||||
|
async for j in communicate.run(
|
||||||
|
i[2], codec="audio-24khz-48kbitrate-mono-mp3", voice=voice_name
|
||||||
|
):
|
||||||
|
if j[2] is not None:
|
||||||
|
f.write(j[2])
|
||||||
|
|
||||||
|
temporary_file = tempfile.NamedTemporaryFile(
|
||||||
|
suffix=".mp3", delete=False
|
||||||
)
|
)
|
||||||
finally:
|
try:
|
||||||
shutil.move(temporary_file2.name, temporary_file.name)
|
ensure_audio_length(fname, temporary_file.name, duration)
|
||||||
|
finally:
|
||||||
|
shutil.move(temporary_file.name, fname)
|
||||||
|
|
||||||
temporary_file2 = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
|
ffmpeg_opts = []
|
||||||
try:
|
for i in range(len(input_files)):
|
||||||
subprocess.call(
|
ffmpeg_opts.append("-i")
|
||||||
[
|
ffmpeg_opts.append(input_files[i])
|
||||||
"ffmpeg",
|
|
||||||
"-y",
|
filter_complex = ""
|
||||||
"-i",
|
for i in range(len(input_files)):
|
||||||
mother_temp_file.name,
|
filter_complex += (
|
||||||
"-i",
|
f"aevalsrc=0:d={input_files_start[input_files[i]]}[s{i+1}];"
|
||||||
temporary_file.name,
|
)
|
||||||
"-filter_complex",
|
filter_complex += f"[s{i+1}][{i+1}:a]concat=n=2:v=0:a=1[ac{i+1}];"
|
||||||
f"aevalsrc=0:d={start}[s1];[s1][1:a]concat=n=2:v=0:a=1[ac1];[0:a][ac1]amix=2[aout]",
|
|
||||||
"-map",
|
filter_complex += f"[0:a]"
|
||||||
"[aout]",
|
for i in range(len(input_files)):
|
||||||
temporary_file2.name,
|
filter_complex += f"[ac{i+1}]"
|
||||||
],
|
filter_complex += f"amix={len(input_files)+1}[aout]"
|
||||||
stdout=subprocess.DEVNULL,
|
|
||||||
stderr=subprocess.DEVNULL,
|
ffmpeg_opts.append("-filter_complex")
|
||||||
)
|
ffmpeg_opts.append(filter_complex)
|
||||||
finally:
|
ffmpeg_opts.append("-map")
|
||||||
shutil.move(temporary_file2.name, mother_temp_file.name)
|
ffmpeg_opts.append("[aout]")
|
||||||
|
|
||||||
|
temporary_file2 = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
|
||||||
|
try:
|
||||||
|
print("Concatenating...")
|
||||||
|
subprocess.call(
|
||||||
|
["ffmpeg", "-y", "-i", mother_temp_file.name]
|
||||||
|
+ ffmpeg_opts
|
||||||
|
+ [temporary_file2.name],
|
||||||
|
stdout=subprocess.DEVNULL,
|
||||||
|
stderr=subprocess.DEVNULL,
|
||||||
|
)
|
||||||
|
finally:
|
||||||
|
shutil.move(temporary_file2.name, mother_temp_file.name)
|
||||||
finally:
|
finally:
|
||||||
shutil.move(mother_temp_file.name, out_file)
|
shutil.move(mother_temp_file.name, out_file)
|
||||||
|
print("Done")
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|||||||
Reference in New Issue
Block a user