Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aksinghdce/12dab83d9bf9b5218f018801654d8a4a to your computer and use it in GitHub Desktop.
Save aksinghdce/12dab83d9bf9b5218f018801654d8a4a to your computer and use it in GitHub Desktop.
Get GPS and Other Data from Image taken from Camera

Look at the method : def readExif(self) of class BlenderVideoEditor:

I needed to install exifread. To install exifread, I had to located the python binary in blender installation folder. From there I ran the command

./python3.10 -m ensurepip --upgrade

./pip3.10 install exifread

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 = -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
    def readExif(self):
        '''Read GPS data from images'''
        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))
        print(tags[0])

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/DCIM/Camera/jan-feb-2022')
    #bve.rotate_clip(clip=bpy.context.scene.sequence_editor.sequences["16"])
#    for i in [0, 1, 9, 16, 17, 27, 28, 29, 30, 31, 34, 35, 45]:
#        bve.rotate_and_descale_clip(clip=bpy.context.scene.sequence_editor.sequences[str(i)])    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment