Skip to content

Instantly share code, notes, and snippets.

@arifsuhan
Last active July 9, 2023 11:30
Show Gist options
  • Save arifsuhan/385b36a4c5649be355431cf7d4c4e8a0 to your computer and use it in GitHub Desktop.
Save arifsuhan/385b36a4c5649be355431cf7d4c4e8a0 to your computer and use it in GitHub Desktop.
!pip install moviepy
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
from moviepy.editor import *
from IPython.display import HTML
from base64 import b64encode
from os import path

class video_master:

  def __init__(self, file_path, save_path="test.mp4"):
    self.file_path = file_path
    self.save_path = save_path

  def vide_to_html(self,path):
    mp4 = open(path,'rb').read()
    data_url = "data:video/mp4;base64," + b64encode(mp4).decode()
    return HTML("""
        <video width=1100 height=500 controls>
              <source src="%s" type="video/mp4">
        </video>
    """ % data_url)

  def view(self):
    return self.vide_to_html(self.file_path)

  def trim(self,start_time,end_time,show=False):
    ffmpeg_extract_subclip(self.file_path, 
                          start_time, 
                          end_time, 
                          targetname= self.save_path)
    
    if show and path.exists(self.save_path):
      return self.vide_to_html(self.save_path)

  def n_times(self,n):
    raw = VideoFileClip(self.file_path)
    # clip = clip.subclip(0, 4)
    clip_arr = [ raw for x in range(n)]
    final_clip = concatenate_videoclips(clip_arr)
    final_clip.write_videofile("test2.mp4")
  
    return self.vide_to_html("test2.mp4")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment