From 764530e6646e9d06426f4cb4aca1388cf099451b Mon Sep 17 00:00:00 2001 From: Syed Aman Raza <109358640+electro199@users.noreply.github.com> Date: Tue, 11 Oct 2022 13:32:38 +0500 Subject: [PATCH] feat:Added AI based sentence processing --- utils/imagenarator.py | 4 ++- utils/posttextparser.py | 60 +++++++++++------------------------ video_creation/final_video.py | 14 ++++---- 3 files changed, 30 insertions(+), 48 deletions(-) diff --git a/utils/imagenarator.py b/utils/imagenarator.py index 1b5ccf3..d08bd5e 100644 --- a/utils/imagenarator.py +++ b/utils/imagenarator.py @@ -1,6 +1,8 @@ from PIL import Image, ImageDraw,ImageFont import textwrap import re + +from rich.progress import track def draw_multiple_line_text(image, text, font, text_color,padding): ''' Draw multiline text over given image @@ -27,7 +29,7 @@ def imagemaker( theme,reddit_obj, size=(500,176) textcolor=txtclr - for idx,text in enumerate(texts): + for idx,text in track(enumerate(texts),'Rendering Imaging..'): image =Image.new('RGBA',size,theme) draw = ImageDraw.Draw(image) if len(text)>50: diff --git a/utils/posttextparser.py b/utils/posttextparser.py index a0d936c..287e07f 100644 --- a/utils/posttextparser.py +++ b/utils/posttextparser.py @@ -1,46 +1,24 @@ - -MAX_CHARACTER= 200 +import re +import spacy #working good def posttextparser(obj): - text=obj#["thread_post"] - newtext=[] - # for text in text: - if len(text)>MAX_CHARACTER: - text2=text.split("\n") - for dot in text2: - if len(dot)>MAX_CHARACTER: - text3=dot.split(".") - for comma in text3: - if len(comma)> MAX_CHARACTER: - text4=comma.split(',') - newtext.extend(text4) - else: - newtext.append(comma) - else: - newtext.append(dot) - else: - newtext.append(text) - return remover(newtext) -def remover(List): - reg=['',' ','.','\n',')',"''",'"',"'",'"','""'] #add if any any unwant value found - lines=List - lines1=[] - lines2=[] - - for item in lines: - for r in reg : - if item==r: - break - else: - continue - else: - lines1.append(item) + text=re.sub("\n", "", obj ) + + try: + nlp=spacy.load('en_core_web_sm') + except OSError : + + print("dev:please dowload the model with this command \npython -m spacy download en") + exit() - for a in lines1: #Double check - if a!='': - aa=a.strip() - lines2.append(aa) - # print(f'"{a}"') - return lines2 \ No newline at end of file + doc= nlp(text) + + newtext=[] + + for line in doc.sents: + newtext.append(line.text) + # print(line) + + return newtext \ No newline at end of file diff --git a/video_creation/final_video.py b/video_creation/final_video.py index 5634e85..f1481a9 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -4,7 +4,6 @@ import os import re from os.path import exists from typing import Tuple, Any - from moviepy.audio.AudioClip import concatenate_audioclips, CompositeAudioClip from moviepy.audio.io.AudioFileClip import AudioFileClip from moviepy.video.VideoClip import ImageClip @@ -13,15 +12,16 @@ from moviepy.video.compositing.concatenate import concatenate_videoclips from moviepy.video.io.VideoFileClip import VideoFileClip from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip from rich.console import Console +from rich.progress import track -from utils import settings from utils.cleanup import cleanup from utils.console import print_step, print_substep from utils.video import Video from utils.videos import save_data +from utils import settings console = Console() -W, H = 1080, 1920 +W, H = 1080,1920 def name_normalize(name: str) -> str: @@ -83,7 +83,7 @@ def make_final_video( audio_clips.insert(1,AudioFileClip(f"assets/temp/{id}/mp3/postaudio.mp3")) elif settings.config["settings"]["storymodemethod"] == 1: #here work is not done14 - audio_clips = [AudioFileClip(f"assets/temp/{id}/mp3/postaudio-{i}.mp3") for i in range(number_of_clips+1)] + audio_clips = [AudioFileClip(f"assets/temp/{id}/mp3/posttext-{i}.mp3") for i in track(range(number_of_clips+1),"Collecting the audio files...")] audio_clips.insert(0, AudioFileClip(f"assets/temp/{id}/mp3/title.mp3")) else: @@ -119,7 +119,7 @@ def make_final_video( .set_opacity(float(opacity)), ) elif settings.config["settings"]["storymodemethod"] == 1: - for i in range(0, number_of_clips+1): + for i in track(range(0, number_of_clips+1),"Collecting the image files..."): image_clips.append( ImageClip(f"assets/temp/{id}/png/img{i}.png") .set_duration(audio_clips[i + 1].duration) @@ -142,7 +142,9 @@ def make_final_video( img_clip_pos = background_config[3] - image_concat = concatenate_videoclips(image_clips).set_position(img_clip_pos) # note transition kwarg for delay in imgs + image_concat = concatenate_videoclips(image_clips).set_position( + img_clip_pos + ) # note transition kwarg for delay in imgs image_concat.audio = audio_composite final = CompositeVideoClip([background_clip, image_concat]) title = re.sub(r"[^\w\s-]", "", reddit_obj["thread_title"])