Question 1: In the code you'd observe there's a lot of room for improvement. Can you spot those and write them in the comment?
import bpy
from pathlib import Path
from datetime import datetime
from datetime import timezone
import math
import exifread
from PIL import Image
import logging
# 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 = -444
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=1, 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 = 1
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.text = str(self.readExif(filename=str(x[0])))
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=ch-annel-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 = 1
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; " + str(self.readExif(filename=str(x[0])))
subtitle_clip.text = str(self.readExif(filename=str(x[0])))
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=-444
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
def readExif(self, filename=None):
'''Read GPS data from images'''
result = dict()
if None == filename:
tags=list()
for i, x in enumerate(self.get_video_file_names(dir='/media/amit/3662-3532/DCIM/Camera/jan-feb-2022')):
with open(str(x[0]), "rb") as f:
tags.append(exifread.process_file(f, details=False))
if "GPS GPSLatitude" in tags[0]:
print("GPSLatitude:")
print(tags[0]["GPS GPSLatitude"])
if "GPS GPSLongitude" in tags[0]:
print("GPSLongitude:")
print(tags[0]["GPS GPSLongitude"])
else:
with open(filename, 'rb') as f:
tags=exifread.process_file(f, details=False)
if "GPS GPSLatitude" in tags:
result["GPSLatitude"] = tags["GPS GPSLatitude"]
#
# print("GPSLatitude:")
# print(tags["GPS GPSLatitude"])
if "GPS GPSLongitude" in tags:
result["GPSLongitude"] = tags["GPS GPSLongitude"]
# print("GPSLongitude:")
# print(tags["GPS GPSLongitude"])
if "Image DateTime" in tags:
result["DateTime"] = tags["Image DateTime"]
return result
if __name__ == '__main__':
# Select Aswal's wedding photos and videos
bve = BlenderVideoEditor()
# bve.readExif()
# 37716
bve.add_movie_clips_from(dir='/media/amit/3662-3532/Movies/Classes-nephews')
#bve.rotate_clip(clip=bpy.context.scene.sequence_editor.sequences["16"])
# for i in [0, 5, 7, 9, 11, 12, 13, 14, 15, 20, 26, 27, 28, 29, 31, 32, 34, 35, 36, 41]:
# bve.rotate_and_descale_clip(clip=bpy.context.scene.sequence_editor.sequences[str(i)])