Skip to content

Instantly share code, notes, and snippets.

@aksinghdce
Created April 3, 2023 19:03
Show Gist options
  • Save aksinghdce/45fbfa9e189df46002d358a29b1b7260 to your computer and use it in GitHub Desktop.
Save aksinghdce/45fbfa9e189df46002d358a29b1b7260 to your computer and use it in GitHub Desktop.
4K Videos from Camera DCIM folders
import bpy
from pathlib import Path
from datetime import datetime
from datetime import timezone
import math
# sequence_editor.sequences_all["178"].transform.rotation
# Song : https://www.youtube.com/watch?v=SKahWKxIzYI
class BlenderVideoEditor:
    def __init__(self):
        '''initialize start_frame and duration of video-synced-audio'''
        self.vsa="/media/amit/4CFC-8D04/DCIM/1"
        self.start_n_dur=list()
    def enable_scale_keyframe(self, clip=None, scale=1, frame=1):
        clip.transform.scale_x = scale
        clip.transform.scale_y = scale
        clip.transform.keyframe_insert(data_path="scale_x", frame=frame)
        clip.transform.keyframe_insert(data_path="scale_y", frame=frame)
        return clip
    def enable_rotate_keyframe(self, clip=None, frame_s=1, frame_e=10):
        clip.transform.rotation = 0
        clip.transform.keyframe_insert(data_path="rotation", frame=frame_s)
        clip.transform.rotation = (-1 * math.pi / 2)
        clip.transform.keyframe_insert(data_path="rotation", frame=frame_e)
        return clip
    def get_video_file_names(self, dir="/media/amit/4CFC-8D04/DCIM/1"):
        p = Path(dir)
        return [(x.absolute(), datetime.fromtimestamp(x.stat().st_mtime)) for x in sorted(p.iterdir()) if not x.is_dir()]
    def add_movie_clips_from(self, dir="/media/amit/4CFC-8D04/DCIM/2", start_frame=1, channel=2):
        frame_s = start_frame
        y_offset = -1277
        for i, x in enumerate(self.get_video_file_names(dir=dir)):
            if str(x[0]).endswith(".mp4"):
                clip_v = bpy.context.scene.sequence_editor.sequences.new_movie(name=str(i),filepath=str(x[0]),channel=channel, frame_start = frame_s)
                clip_a = bpy.context.scene.sequence_editor.sequences.new_sound(name=str(i),filepath=str(x[0]),channel=channel+1, frame_start = frame_s)
                dur = max([clip_v.frame_duration, clip_a.frame_duration])
                clip_v = self.enable_scale_keyframe(clip=clip_v, scale=1, frame=frame_s)
                clip_v = self.enable_scale_keyframe(clip=clip_v, scale=2.5, frame=frame_s + dur)
                self.start_n_dur.append((frame_s, dur))
                #clip_v = self.enable_rotate_keyframe(clip=clip_v, frame_s=frame_s, frame_e=frame_s + dur)
                timestring = datetime.now().strftime("%f")
                color_clip = bpy.context.scene.sequence_editor.sequences.new_effect(name="color"+timestring, type="COLOR", channel = channel+2, frame_start = frame_s, frame_end = frame_s + dur)
                color_clip.color = (0.5, 0.5, 0.5)
                color_clip.blend_type = 'ALPHA_OVER'
                color_clip.blend_alpha = 0.5
                color_clip.transform.scale_y = 0.1
                color_clip.transform.scale_x = 0.9
                color_clip.transform.offset_y = y_offset
                subtitle_clip = bpy.context.scene.sequence_editor.sequences.new_effect(name="subtitle"+timestring, type="TEXT", channel = channel+3, frame_start = frame_s, frame_end = frame_s + dur)
                subtitle_clip.text = str(x[1]) + " UTC"
                subtitle_clip.transform.offset_y= y_offset
                subtitle_clip.font_size = 64
                frame_s = frame_s + dur
            if str(x[0]).endswith(".jpg"):
                clip_i = bpy.context.scene.sequence_editor.sequences.new_image(name=str(i), filepath=str(x[0]), channel=channel-1, frame_start = frame_s)
                clip_i.frame_final_duration = 240
                #clip_i = self.enable_rotate_keyframe(clip=clip_i, frame_s=frame_s, frame_e=frame_s + 240)
                timestring = datetime.now().strftime("%f")
                color_clip = bpy.context.scene.sequence_editor.sequences.new_effect(name="color"+timestring, type="COLOR", channel = channel+2, frame_start = frame_s, frame_end = frame_s + 240)
                color_clip.color = (0.5, 0.5, 0.5)
                color_clip.blend_type = 'ALPHA_OVER'
                color_clip.blend_alpha = 0.5
                color_clip.transform.scale_y = 0.1
                color_clip.transform.scale_x = 0.9
                color_clip.transform.offset_y = y_offset
                subtitle_clip = bpy.context.scene.sequence_editor.sequences.new_effect(name="subtitle"+timestring, type="TEXT", channel = channel+3, frame_start = frame_s, frame_end = frame_s + 240)
                subtitle_clip.text = str(x[1]) + " UTC"
                subtitle_clip.transform.offset_y= y_offset
                subtitle_clip.font_size = 64
                frame_s = frame_s + 240
    def add_text_strip(self):
        '''Add text strip to the video sequence'''
        path="///home/amit/Documents/pendrive/Career-Template/7-podcasts-blender/2-personal-youtube-channel/Diplomatic-Dispatch-Series-Sansad-Tv/LabelTrack.txt"
        p = Path(path)
        lines = list()
        with open(p, "r",  encoding='utf-8-sig') as f:
            lines = f.readlines()
        ts = None
        for l in lines:
            la = l.split("\t")
            fs = (int(float(la[0].strip())) + 1) * 15
            fe = ((int(float(la[1].strip())) + 1) * 15 ) + 3
            ts = context.scene.sequence_editor.sequences.new_effect(name="subtitle", type="TEXT", channel=6, frame_start = fs, frame_end = fe)
            ts = context.scene.sequence_editor.sequences.new_effect(name="subtitle", type="TEXT", channel=6, frame_start = fs, frame_end = fe)
            ts = context.scene.sequence_editor.sequences.new_effect(name="subtitle", type="TEXT", channel=6, frame_start = fs, frame_end = fe)
            ts.text = l.split("\t")[2].strip()
            ts.transform.offset_y=-377
            ts.font_size = 42
    def rotate_and_descale_clip(self, clip=None):
        # rotate thhe given clip by - pi / 2
        clip.transform.rotation = (-1 * math.pi / 2)
        clip.transform.scale_x = 0.75
        clip.transform.scale_y = 0.75

if __name__ == '__main__':
    # Select Aswal's wedding photos and videos
    bve = BlenderVideoEditor()
    # 33687
    #bve.add_movie_clips_from(dir='/media/amit/3662-3532/DCIM/Camera/jul-aug-sep-2021/Jul-2021/2')
    #bve.rotate_clip(clip=bpy.context.scene.sequence_editor.sequences["16"])
    bve.rotate_and_descale_clip(clip=bpy.context.scene.sequence_editor.sequences["69"])    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment