Merge pull request #830 from elebumm/fix/moviepy-length-checker

Check clip length using moviepy
pull/863/head
Jason 2 years ago committed by GitHub
commit ae25854de6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -4,9 +4,9 @@ from typing import Tuple
import re import re
from os import getenv from os import getenv
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 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
@ -94,12 +94,10 @@ class TTSEngine:
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")
try: # try:
try: # self.length += MP3(f"{self.path}/{filename}.mp3").info.length
self.length += MP3(f"{self.path}/{filename}.mp3").info.length # except (MutagenError, HeaderNotFoundError):
except (MutagenError, HeaderNotFoundError): # self.length += sox.file_info.duration(f"{self.path}/{filename}.mp3")
self.length += sox.file_info.duration(f"{self.path}/{filename}.mp3")
except Exception: # last resort backup
clip = AudioFileClip(f"{self.path}/{filename}.mp3") clip = AudioFileClip(f"{self.path}/{filename}.mp3")
self.length += clip.duration self.length += clip.duration

@ -1,5 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
import math
from subprocess import Popen from subprocess import Popen
from os import getenv, name from os import getenv, name
from dotenv import load_dotenv from dotenv import load_dotenv
@ -35,6 +35,7 @@ def main(POST_ID=None):
cleanup() cleanup()
reddit_object = get_subreddit_threads(POST_ID) reddit_object = get_subreddit_threads(POST_ID)
length, number_of_comments = save_text_to_mp3(reddit_object) length, number_of_comments = save_text_to_mp3(reddit_object)
length = math.ceil(length)
download_screenshots_of_reddit_posts(reddit_object, number_of_comments) download_screenshots_of_reddit_posts(reddit_object, number_of_comments)
download_background() download_background()
chop_background_video(length) chop_background_video(length)

@ -51,13 +51,7 @@ def make_final_video(number_of_clips: int, length: int, reddit_obj: dict):
audio_concat = concatenate_audioclips(audio_clips) audio_concat = concatenate_audioclips(audio_clips)
audio_composite = CompositeAudioClip([audio_concat]) audio_composite = CompositeAudioClip([audio_concat])
# Get sum of all clip lengths console.log(f"[bold green] Video Will Be: {length} Seconds Long")
total_length = sum([clip.duration for clip in audio_clips])
# round total_length to an integer
int_total_length = round(total_length)
# Output Length
console.log(f"[bold green] Video Will Be: {int_total_length} Seconds Long")
# add title to video # add title to video
image_clips = [] image_clips = []
# Gather all images # Gather all images
@ -114,7 +108,7 @@ def make_final_video(number_of_clips: int, length: int, reddit_obj: dict):
threads=multiprocessing.cpu_count(), threads=multiprocessing.cpu_count(),
) )
ffmpeg_tools.ffmpeg_extract_subclip( ffmpeg_tools.ffmpeg_extract_subclip(
"assets/temp/temp.mp4", 0, length, targetname=f"results/{subreddit}/{filename}" "assets/temp/temp.mp4", 0, final.duration, targetname=f"results/{subreddit}/{filename}"
) )
# os.remove("assets/temp/temp.mp4") # os.remove("assets/temp/temp.mp4")

Loading…
Cancel
Save