def gifEngine(starttime, endtime, videofileloc, srtfileloc, outfileloc, logger='gifEngine.log'): logging.basicConfig(filename=logger, level=logging.DEBUG) logger = logging.getLogger(__name__) prolog.basic_config() # creating the initial GIF try: generator = lambda txt: TextClip( txt, font='Impact', fontsize=28, color='white') video = VideoFileClip(videofileloc) sub = SubtitlesClip(srtfileloc, generator).set_position( ("center", "bottom"), relative=True) composite = CompositeVideoClip([video, sub]) composite = composite.subclip(starttime, endtime) composite.write_gif(outfileloc, program='ffmpeg', opt='palettegen', logger=logger, verbose=True) # using new palettegen opt return 0 except (IOError, OSError) as err: return err
def prepend_intertitle( self, size: Optional[Size] = None, color: str = DEFAULT_INTERTITLE_COLOR, font: str = DEFAULT_INTERTITLE_FONT, fontsize: int = DEFAULT_INTERTITLE_FONTSIZE, position: str = DEFAULT_INTERTITLE_POSITION, duration: int = DEFAULT_INTERTITLE_DURATION, ): if not self.meta.text: logger.warning('%s: Missing intertitle text') return logger.info('%s: Intertitle "%s"', self.meta.path, self.meta.text) if not size: size = Size(width=self.video_file_clip.w, height=self.video_file_clip.h) text_clip = TextClip( self.meta.text.replace('|', '\n'), size=(size.width * INTERTITLE_TEXT_WIDTH_FACTOR, None), color=color, font=font, fontsize=fontsize, method='caption', align='center', ) composite_clip = CompositeVideoClip([text_clip.set_pos(position)], (size.width, size.height)) intertitle_clip = composite_clip.subclip(0, duration) self.video_file_clip = concatenate_videoclips( [intertitle_clip, self.video_file_clip], method='compose')
def construct_subclip(index, my_text, video, id): ukulele = video w, h = moviesize = ukulele.size txt = TextClip(my_text, font='DejaVu-Serif', color='white', fontsize=24) txt_col = txt.on_color(size=(ukulele.w + txt.w, txt.h + 10), color=(0, 0, 0), pos=(6, 'center'), col_opacity=0.6) txt_mov = txt_col.set_pos(lambda t: (max(w / 30, int(w - 0.5 * w * t)), max(5 * h / 6, int(100 * t)))) result = CompositeVideoClip([video, txt_mov]) os.makedirs(store_path + "/session" + str(id), exist_ok=True) url = store_path + "/session" + str(id) + "/sample_edited" + str( index) + ".mp4" # Overlay text on video result.subclip(0, 10).write_videofile(url) # Many options... view_url = request_url + "/videos/" + "/session" + str( id) + "/sample_edited" + str(index) + ".mp4" return view_url
def filter_add_intertitle( video_clip, text, color, font, fontsize, position, duration, width, height): text_clip = generate_text_clip( text, width * TEXT_WIDTH_FACTOR, color=color, font=font, fontsize=fontsize) composite_clip = CompositeVideoClip( [text_clip.set_pos(position)], (width, height)) intertitle_clip = composite_clip.subclip(0, duration) return concatenate_videoclips( [intertitle_clip, video_clip], method='compose')
def build_main(self, clip, max_duration: int = 0, font: str = 'Arial', fontsize: int = 100, color: str = 'white', link: str = "") -> StoryBuild: """ Build clip Parameters ---------- clip: (VideoFileClip, ImageClip) An object of either VideoFileClip or ImageClip max_duration: int, optional Duration of the clip if a video clip, default value is 0 font: str, optional Name of font for text clip fontsize: int, optional Size of font color: str, optional Color of text Returns ------- StoryBuild An object of StoryBuild """ clips = [] stickers = [] # Background if self.bgpath: assert self.bgpath.exists( ), f"Wrong path to background {self.bgpath}" background = ImageClip(str(self.bgpath)) clips.append(background) # Media clip clip_left = (self.width - clip.size[0]) / 2 clip_top = (self.height - clip.size[1]) / 2 if clip_top > 90: clip_top -= 50 media_clip = clip.set_position((clip_left, clip_top)) clips.append(media_clip) mention = self.mentions[0] if self.mentions else None # Text clip caption = self.caption if self.mentions: mention = self.mentions[0] if getattr(mention, 'user', None): caption = f"@{mention.user.username}" if caption: text_clip = TextClip( caption, color=color, font=font, kerning=-1, fontsize=fontsize, method="label", ) text_clip_left = (self.width - 600) / 2 text_clip_top = clip_top + clip.size[1] + 50 offset = (text_clip_top + text_clip.size[1]) - self.height if offset > 0: text_clip_top -= offset + 90 text_clip = (text_clip.resize(width=600).set_position( (text_clip_left, text_clip_top)).fadein(3)) clips.append(text_clip) if link: url = urlparse(link) link_clip = TextClip( url.netloc, color="blue", bg_color="white", font=font, kerning=-1, fontsize=32, method="label", ) link_clip_left = (self.width - 400) / 2 link_clip_top = clip.size[1] / 2 link_clip = (link_clip.resize(width=400).set_position( (link_clip_left, link_clip_top)).fadein(3)) link_sticker = StorySticker( # x=160.0, y=641.0, z=0, width=400.0, height=88.0, x=round(link_clip_left / self.width, 7), # e.g. 0.49953705 y=round(link_clip_top / self.height, 7), # e.g. 0.5 z=0, width=round(link_clip.size[0] / self.width, 7), # e.g. 0.50912 height=round(link_clip.size[1] / self.height, 7), # e.g. 0.06875 rotation=0.0, # id="link_sticker_default", type="story_link", extra=dict( link_type="web", url=str(link), # e.g. "https//github.com/" tap_state_str_id="link_sticker_default", )) stickers.append(link_sticker) clips.append(link_clip) # Mentions mentions = [] if mention: mention.x = 0.49892962 # approximately center mention.y = (text_clip_top + text_clip.size[1] / 2) / self.height mention.width = text_clip.size[0] / self.width mention.height = text_clip.size[1] / self.height mentions = [mention] duration = max_duration if getattr(clip, 'duration', None): if duration > int(clip.duration) or not duration: duration = int(clip.duration) destination = tempfile.mktemp(".mp4") cvc = CompositeVideoClip(clips, size=(self.width, self.height))\ .set_fps(24)\ .set_duration(duration) cvc.write_videofile(destination, codec="libx264", audio=True, audio_codec="aac") paths = [] if duration > 15: for i in range(duration // 15 + (1 if duration % 15 else 0)): path = tempfile.mktemp(".mp4") start = i * 15 rest = duration - start end = start + (rest if rest < 15 else 15) sub = cvc.subclip(start, end) sub.write_videofile(path, codec="libx264", audio=True, audio_codec="aac") paths.append(path) return StoryBuild(mentions=mentions, path=destination, paths=paths, stickers=stickers)
job = fake.job() name = fake.name() if len(job) < 25: text.append((job, name)) def make_frame(t): """Draw text elements in each frame""" surface = gz.Surface(W, H, bg_color=(0,0,0)) for i, line in enumerate(text): job, name = line ypos = LINE_HEIGHT * i - int(t * SCROLLSPEED) + BOTTOM_START txt = gz.text(job, "Amiri", TEXTSIZE, fontweight='bold', \ h_align='right', fill=(1,1,1)) left = gz.Group([txt]).translate((LEFTCOL, ypos)) left.draw(surface) txt = gz.text(name, "Amiri", TEXTSIZE, \ fontweight='normal',h_align='left', fill=(1,1,1)) right = gz.Group([txt]).translate((RIGHTCOL, ypos)) right.draw(surface) return surface.get_npimage() clip = mpy.VideoClip(make_frame, duration=DURATION) # mix text and logo together final = CompositeVideoClip([clip, LOGO]) final.subclip(0, DURATION).write_videofile("abspann.avi", codec="h264", fps=24) #final.subclip(0, DURATION).write_videofile("abspann.mp4", codec="mpeg4", fps=24)
videoLength = videoOrig.duration print(videoLength) colors = ["red", "green", "blue", "yellow", "purple", "orange"] shuffle(colors) ar = segmentDivider(videoLength, 3, 40) for q in range(len(ar)): print(q) videoText = (TextClip(str(randint(1000, 10000)), font="Adobe-Gothic-Std", fontsize=150, color=colors[q]) .set_position(("right", "top")) # (optional) logo-border padding .margin(right=50, top=50, opacity=0) .set_duration(numCodeDuration) .set_start(randint(ar[q][0], (ar[q][1]-numCodeDuration))) .crossfadein(0.5) .crossfadeout(0.5) ) watermark = (ImageClip(r"watermark.png") .set_position(("left", "bottom")) .margin(left=25, bottom=25, opacity=0) .set_duration(videoOrig.duration) ) videoOverlay = CompositeVideoClip([videoOrig, videoText, watermark]) videoOverlay.subclip(ar[q][0], ar[q][1]).write_videofile( "videoOverlay"+str(q)+".mp4")