@ -2,21 +2,15 @@
from pathlib import Path
from pathlib import Path
from typing import Tuple
from typing import Tuple
import re
import re
<< << << < Updated upstream
# import sox
# import sox
# from mutagen import MutagenError
# from mutagen import MutagenError
# from mutagen.mp3 import MP3, HeaderNotFoundError
# from mutagen.mp3 import MP3, HeaderNotFoundError
== == == =
import os
import os
from os import getenv
import numpy as np
import numpy as np
import ffmpeg
from moviepy . audio . AudioClip import AudioClip
from moviepy . audio . AudioClip import AudioClip
from moviepy . audio . fx . volumex import volumex
from moviepy . audio . fx . volumex import volumex
from mutagen . mp3 import MP3
>> >> >> > Stashed changes
import translators as ts
import translators as ts
from rich . progress import track
from rich . progress import track
from moviepy . editor import AudioFileClip , CompositeAudioClip , concatenate_audioclips
from moviepy . editor import AudioFileClip , CompositeAudioClip , concatenate_audioclips
@ -24,7 +18,7 @@ from utils.console import print_step, print_substep
from utils . voice import sanitize_text
from utils . voice import sanitize_text
from utils import settings
from utils import settings
DEF U ALT_MAX_LENGTH: int = 50 # video length variable
DEF AU LT_MAX_LENGTH: int = 50 # video length variable
class TTSEngine :
class TTSEngine :
@ -46,7 +40,7 @@ class TTSEngine:
tts_module ,
tts_module ,
reddit_object : dict ,
reddit_object : dict ,
path : str = " assets/temp/mp3 " ,
path : str = " assets/temp/mp3 " ,
max_length : int = DEF U ALT_MAX_LENGTH,
max_length : int = DEF AU LT_MAX_LENGTH,
) :
) :
self . tts_module = tts_module ( )
self . tts_module = tts_module ( )
self . reddit_object = reddit_object
self . reddit_object = reddit_object
@ -87,177 +81,34 @@ class TTSEngine:
return self . length , idx
return self . length , idx
def split_post ( self , text : str , idx : int ) :
def split_post ( self , text : str , idx : int ) :
split_files = [ ]
split_text = [
split_text = [
x . group ( ) . strip ( )
x . group ( ) . strip ( )
for x in re . finditer ( rf " *((. {{ 0, { self . tts_module . max_chars } }} )( \ .|.$)) " , text )
for x in re . finditer ( rf " *((. {{ 0, { self . tts_module . max_chars } }} )( \ .|.$)) " , text )
]
]
silence_duration = settings . config [ " settings " ] [ " tts " ] [ " silence_duration " ]
silence_long = AudioClip ( make_frame = lambda t : np . sin ( 440 * 2 * np . pi * t ) , duration = int ( getenv ( " COMMENT_GAP " ) ) , fps = 44100 )
silence_long = AudioClip ( make_frame = lambda t : np . sin ( 440 * 2 * np . pi * t ) , duration = silence_duration , fps = 44100 )
silence_long_new = volumex ( silence_long , 0 )
silence_long_new = volumex ( silence_long , 0 )
silence_long_new . write_audiofile ( f " { self . path } /long_silence.mp3 " , fps = 44100 , verbose = False , logger = None )
silence_long_new . write_audiofile ( f " { self . path } /long_silence.mp3 " , fps = 44100 , verbose = False , logger = None )
bruz = [ ]
idy = None
idy = None
for idy , text_cut in enumerate ( split_text ) :
for idy , text_cut in enumerate ( split_text ) :
# print(f"{idx}-{idy}: {text_cut}\n")
# print(f"{idx}-{idy}: {text_cut}\n")
self . call_tts ( f " { idx } - { idy } .part " , text_cut )
self . call_tts ( f " { idx } - { idy } .part " , text_cut )
split_files . append ( AudioFileClip ( f " { self . path } / { idx } - { idy } .part.mp3 " ) )
<< << << < Updated upstream
CompositeAudioClip ( [ concatenate_audioclips ( split_files ) ] ) . write_audiofile (
f " { self . path } / { idx } .mp3 " , fps = 44100 , verbose = False , logger = None
)
for i in split_files :
with open ( f " { self . path } /list.txt " , ' w ' ) as f :
name = i . filename
for newy in range ( 0 , len ( split_text ) ) :
i . close ( )
f . write ( " file " + f " ' { idx } - { newy } .part.mp3 ' " + " \n " )
Path ( name ) . unlink ( )
f . write ( " file " + f " ' long_silence.mp3 ' " + " \n " )
# for i in range(0, idy + 1):
os . system ( " ffmpeg -f concat -y -hide_banner -loglevel panic -safe 0 " +
# print(f"Cleaning up {self.path}/{idx}-{i}.part.mp3")
" -i " + f " { self . path } /list.txt " +
" -c copy " + f " { self . path } / { idx } .mp3 " )
# Path(f"{self.path}/{idx}-{i}.part.mp3").unlink()
== == == =
bruz . append ( ffmpeg . input ( f " { self . path } / { idx } - { idy } .part.mp3 " ) )
if idy == len ( split_text ) - 1 :
split_files . append ( AudioFileClip ( f " { self . path } /long_silence.mp3 " ) )
# print("long silence added")
bruz . append ( ffmpeg . input ( f " { self . path } /long_silence.mp3 " ) )
# with open(f"{self.path}/list.txt", 'w') as f:
# for newy in range(0, len(split_text)):
# f.write(f"{self.path}/{idx}-{newy}.part.mp3"+"\n")
# f.write(f"{self.path}/long_silence.mp3")
if len ( bruz ) == 1 :
stupid0 = bruz [ 0 ]
if len ( bruz ) == 2 :
stupid0 = bruz [ 0 ]
stupid1 = bruz [ 1 ]
if len ( bruz ) == 3 :
stupid0 = bruz [ 0 ]
stupid1 = bruz [ 1 ]
stupid2 = bruz [ 2 ]
if len ( bruz ) == 4 :
stupid0 = bruz [ 0 ]
stupid1 = bruz [ 1 ]
stupid2 = bruz [ 2 ]
stupid3 = bruz [ 3 ]
if len ( bruz ) == 5 :
stupid0 = bruz [ 0 ]
stupid1 = bruz [ 1 ]
stupid2 = bruz [ 2 ]
stupid3 = bruz [ 3 ]
stupid4 = bruz [ 4 ]
if len ( bruz ) == 5 :
stupid0 = bruz [ 0 ]
stupid1 = bruz [ 1 ]
stupid2 = bruz [ 2 ]
stupid3 = bruz [ 3 ]
stupid4 = bruz [ 4 ]
stupid5 = bruz [ 5 ]
if len ( bruz ) == 1 :
try :
ffmpeg \
. concat (
stupid0 ,
v = 0 , a = 1
) \
. output ( f " { self . path } / { idx } .m4a " ) \
. run ( capture_stdout = True , capture_stderr = True )
except ffmpeg . Error as e :
print ( ' stdout: ' , e . stdout . decode ( ' utf8 ' ) )
print ( ' stderr: ' , e . stderr . decode ( ' utf8 ' ) )
raise e
elif len ( bruz ) == 2 :
try :
ffmpeg \
. concat (
stupid0 ,
stupid1 ,
v = 0 , a = 1
) \
. output ( f " { self . path } / { idx } .m4a " ) \
. run ( capture_stdout = True , capture_stderr = True )
except ffmpeg . Error as e :
print ( ' stdout: ' , e . stdout . decode ( ' utf8 ' ) )
print ( ' stderr: ' , e . stderr . decode ( ' utf8 ' ) )
raise e
elif len ( bruz ) == 3 :
try :
ffmpeg \
. concat (
stupid0 ,
stupid1 ,
stupid2 ,
v = 0 , a = 1
) \
. output ( f " { self . path } / { idx } .m4a " ) \
. run ( capture_stdout = True , capture_stderr = True )
except ffmpeg . Error as e :
print ( ' stdout: ' , e . stdout . decode ( ' utf8 ' ) )
print ( ' stderr: ' , e . stderr . decode ( ' utf8 ' ) )
raise e
elif len ( bruz ) == 4 :
try :
ffmpeg \
. concat (
stupid0 ,
stupid1 ,
stupid2 ,
stupid3 ,
v = 0 , a = 1
) \
. output ( f " { self . path } / { idx } .m4a " ) \
. run ( capture_stdout = True , capture_stderr = True )
except ffmpeg . Error as e :
print ( ' stdout: ' , e . stdout . decode ( ' utf8 ' ) )
print ( ' stderr: ' , e . stderr . decode ( ' utf8 ' ) )
raise e
elif len ( bruz ) == 5 :
try :
ffmpeg \
. concat (
stupid0 ,
stupid1 ,
stupid2 ,
stupid3 ,
stupid4 ,
v = 0 , a = 1
) \
. output ( f " { self . path } / { idx } .m4a " ) \
. run ( capture_stdout = True , capture_stderr = True )
except ffmpeg . Error as e :
print ( ' stdout: ' , e . stdout . decode ( ' utf8 ' ) )
print ( ' stderr: ' , e . stderr . decode ( ' utf8 ' ) )
raise e
elif len ( bruz ) == 6 :
try :
ffmpeg \
. concat (
stupid0 ,
stupid1 ,
stupid2 ,
stupid3 ,
stupid4 ,
stupid5 ,
v = 0 , a = 1
) \
. output ( f " { self . path } / { idx } .m4a " ) \
. run ( capture_stdout = True , capture_stderr = True )
except ffmpeg . Error as e :
print ( ' stdout: ' , e . stdout . decode ( ' utf8 ' ) )
print ( ' stderr: ' , e . stderr . decode ( ' utf8 ' ) )
raise e
# CompositeAudioClip([concatenate_audioclips(split_files)]).write_audiofile(
# f"{self.path}/{idx}.mp3", fps=44100, verbose=False, logger=None
# )
# for i in range(0, idy + 1):
for i in range ( 0 , idy + 1 ) :
# print(f"Cleaning up {self.path}/{idx}-{i}.part.mp3")
# print(f"Cleaning up {self.path}/{idx}-{i}.part.mp3")
# Path(f"{self.path}/{idx}-{i}.part.mp3").unlink()
Path ( f " { self . path } / { idx } - { i } .part.mp3 " ) . unlink ( )
>> >> >> > Stashed changes
def call_tts ( self , filename : str , text : str ) :
def call_tts ( self , filename : str , text : str ) :
self . tts_module . run ( text = process_text ( text ) , filepath = f " { self . path } / { filename } .mp3 " )
self . tts_module . run ( text = process_text ( text ) , filepath = f " { self . path } / { filename } .mp3 " )