def test_clustering(self): """ Hierachical clustering analysis """ plt.clf() filepath = path_in_medialib(f'{WAV_FILE}.wav') analyst1 = AudioAnalyst(filepath, TEST_SENTENCE) analyst1.analyse() clusters = analyst1.cluster() dendrogram(clusters.astype(float)) plt.savefig(path_in_medialib('test_dendogram.svg'), format='svg')
def from_text(cls, text, language='en-US', rate=0.4, videopath=None): """ Create a video from a text. """ speaker = GoogleSpeaker() audio_filepath = speaker.speak(text, language=language, rate=rate) if not videopath: basename = os.path.splitext(os.path.basename(audio_filepath))[0] filename = f'{basename}{cls.TARGET_EXTENSION}' video_path = path_in_medialib(filename) analyst = AudioAnalyst(audio_filepath, text) analysis = analyst.analyse() image_maker = ImageMaker(analysis) pattern = image_maker.save_images() outdict = {'vcodec': 'h264', 'shortest': None} image = ffmpeg.input(pattern) audio = ffmpeg.input(audio_filepath) try: ffmpeg.output(image, audio, video_path, **outdict).run(capture_stdout=True, capture_stderr=True) return videopath except ffmpeg.Error as e: print('stdout:', e.stdout.decode('utf8')) print('stderr:', e.stderr.decode('utf8')) raise e return None
def speak(self, text, rate=0.8, language='en-US', filename=None, voice_name='en-US-Wavenet-D'): """ Create the audio file from text. """ if not filename: filename = self.filename_from_text(text, rate, language) filepath = path_in_medialib(filename) filepath_wav = f'{filepath}.wav' filepath_mp3 = f'{filepath}.mp3' if os.path.exists(filepath_wav): return filepath_wav synthesis_input = texttospeech.SynthesisInput(text=text) voice = texttospeech.VoiceSelectionParams(language_code=language, name=voice_name) # Select the type of audio file you want returned audio_config = texttospeech.AudioConfig( audio_encoding=texttospeech.AudioEncoding.MP3, speaking_rate=rate) # voice parameters and audio file type response = self.client.synthesize_speech(input=synthesis_input, voice=voice, audio_config=audio_config) # The response's audio_content is binary. with open(filepath_mp3, "wb") as out: out.write(response.audio_content) self.mp3_to_wav(filepath_mp3, filepath_wav) return filepath_wav
def test_upload_success(self): """ Test multiple upload """ video_path_1 = path_in_medialib('what_are_you_up_to?_0.4_en-US.mp4') video_path_2 = path_in_medialib('how_are_you_doing?_0.4_en-US.mp4') video1 = YoutubeVideo( title='What are you up to. Pronunciation and Intonation', file=video_path_1, ) video2 = YoutubeVideo( title='How are you doing. Pronunciation and Intonation', file=video_path_2, ) upload_file(video1) upload_file(video2)
def init_targetdir(self, custom_targetdir=None): """ Init the target folder. """ if custom_targetdir: self.targetdir = custom_targetdir self.is_custom_targetdir = True else: self.targetdir = path_in_medialib(f'maker_{self.id}') if not os.path.exists(self.targetdir): os.makedirs(self.targetdir)
def test_analysis(self): """ Test parametrization """ plt.clf() filepath = path_in_medialib(f'{WAV_FILE}.wav') # not subsampled 86 sample per second analyst1 = AudioAnalyst( filepath, 'Processing 86 / s', samplerate=44100 ) # subsampled 25 samples per second analyst2 = AudioAnalyst(filepath, TEST_SENTENCE) # 15 seconds per second analyst3 = AudioAnalyst( filepath, 'Processing 15 / s', samplerate=15360, hop=1024, ) # subsampled 10 samples per second analyst4 = AudioAnalyst( filepath, 'Processing 10 / s', samplerate=22050, hop=1024 ) analyst1.analyse() analyst2.analyse() analyst3.analyse() analyst4.analyse() axs = plt.subplots(4, 2)[1] analyst1.plot_samples(axs[0][0]) analyst1.plot_histogram(axs[0][1]) analyst2.plot_samples(axs[1][0]) analyst2.plot_histogram(axs[1][1]) analyst3.plot_samples(axs[2][0]) analyst3.plot_histogram(axs[2][1]) analyst4.plot_samples(axs[3][0]) analyst4.plot_histogram(axs[3][1]) plt.savefig(path_in_medialib('test_analyst.svg'), format='svg')
def from_audio(filename, text=None): wav_file = path_in_medialib(filename) analyst = AudioAnalyst(wav_file, text) analysis = analyst.analyse() image_maker = ImageMaker(analysis) pattern = image_maker.save_images() outdict = {'vcodec': 'h264', 'shortest': None} image = ffmpeg.input(pattern) audio = ffmpeg.input(wav_file) video_path = f"{wav_file}.mp4" try: ffmpeg.output(image, audio, video_path, **outdict).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
def test_make_images(self): generator = ImageMaker(self.analysis) images = generator.make_images() images[0].write_to_png(path_in_medialib('test_cairo_frame_0.jpg')) images[4].write_to_png(path_in_medialib('test_cairo_frame_4.jpg'))
def setUp(self): filepath = path_in_medialib(f'{WAV_FILE}.wav') self.analysis = AudioAnalyst(filepath, TEST_SENTENCE).analyse()
def test_meta_video(self): metadata = VideoMaker.get_meta(path_in_medialib(VIDEO_FILE)) return metadata