Commit 0662e1a9 authored by Gihan Jayatilaka's avatar Gihan Jayatilaka

sync

parent ef221954
import sys
import numpy as np
import cv2
import struct
VIDEO_HEIGHT=1080
VIDEO_WIDTH=1920
NO_FRAMES=0
if __name__ == "__main__":
print("python synchronize-time-1.py video-rec.mp4 0 screen-time.ts 0 cam-time.ts 0 output.mp4")
print("python synchronize-time-1.py sys.argv[1] [2] [3] [4] [5] [6] [7] ")
INPUT_VIDEO_FILE=sys.argv[1]
VIDEO_OFFSET=int(sys.argv[2])
SCREEN_TIME_FILE=sys.argv[3]
SCREEN_OFFSET=int(sys.argv[4])
CAMERA_TIME_FILE=sys.argv[5]
CAMERA_OFFSET=int(sys.argv[6])
OUTPUT_VIDEO_FILE=sys.argv[7]
vidIn=cv2.VideoCapture(INPUT_VIDEO_FILE)
ret,fr=vidIn.read()
VIDEO_HEIGHT=fr.shape[0]
VIDEO_WIDTH=fr.shape[1]
NO_FRAMES_VIDEO=int(vidIn.get(cv2.CAP_PROP_FRAME_COUNT))
vidIn.release()
vidIn=cv2.VideoCapture(INPUT_VIDEO_FILE)
fourcc=cv2.VideoWriter_fourcc(*'XVID')
vidOut=cv2.VideoWriter(OUTPUT_VIDEO_FILE,fourcc,60,(VIDEO_WIDTH,VIDEO_HEIGHT))
print("Input video {} of size {}x{} and {} frames".format(INPUT_VIDEO_FILE,VIDEO_HEIGHT,VIDEO_WIDTH,NO_FRAMES_VIDEO))
screenTimeBinFile=open(SCREEN_TIME_FILE,"rb")
cameraTimeBinFile=open(CAMERA_TIME_FILE,"rb")
tempBin = cameraTimeBinFile.read(4)
lenCamTimestamps = struct.unpack("I", tempBin)[0]
print("Number of timestamps: {} as per camera ts (header line)".format(lenCamTimestamps))
tempBin = screenTimeBinFile.read(4)
lenScreenTimestamps = struct.unpack("I", tempBin)[0]
print("Number of timestamps: {} as per screen ts (header line)".format(lenScreenTimestamps ))
cameraTime=[]
screenTime=[]
for i in range(lenCamTimestamps):
tempBin= cameraTimeBinFile.read(8)
timestamp = struct.unpack("L", tempBin)[0]
cameraTime.append(timestamp)
for i in range(lenScreenTimestamps):
tempBin= screenTimeBinFile.read(8)
sec = struct.unpack("L", tempBin)[0]
# screenTimeSec.append(sec)
tempBin= screenTimeBinFile.read(8)
nanosec = struct.unpack("L", tempBin)[0]
nanosec += sec*1000000000
screenTime.append(nanosec)
# print(cameraTime)
print("Len of cameraTime {} as per camera ts (all lines)".format(len(cameraTime)))
print("Len of screenTime {} as per screen ts (all lines)".format(len(screenTime)))
screenTime=screenTime[SCREEN_OFFSET:]
cameraTime=cameraTime[CAMERA_OFFSET:]
for _ in range(VIDEO_OFFSET):
vidIn.read()
print("Screen time ",screenTime[:100])
print("Camera time ",cameraTime[:100])
for i in range(len(screenTime)-1,-1,-1):
screenTime[i]=screenTime[i]-screenTime[0]
for i in range(len(cameraTime)-1,-1,-1):
cameraTime[i]=cameraTime[i]-cameraTime[0]
print("Screen time ",screenTime[:100])
print("Camera time ",cameraTime[:100])
# input("Hi!")
skipCamFrame=[True for _ in range(len(cameraTime))]
# print(skipCamFrame)
camIdx=0
ret,fr=vidIn.read()
for scrIdx in range(len(screenTime)):
print("Staring iteration with camIdx {} for scrIdx {}".format(camIdx,scrIdx))
if camIdx+1 == len(cameraTime):
print("END")
break
while True:
if camIdx+1 == len(cameraTime):
print("screen,cam ",scrIdx,camIdx)
print("The camera time-stamps reached its end before the screen time-stamps end")
print("END")
break
if abs(cameraTime[camIdx]-screenTime[scrIdx]) < abs(cameraTime[camIdx+1]-screenTime[scrIdx]):
# cv2.imshow("AA",fr)
# cv2.waitKey(100)
vidOut.write(fr)
print("Saved camIdx {} for scrIdx {}".format(camIdx,scrIdx))
break
camIdx+=1
ret,fr=vidIn.read()
if not ret:
print("ERROR")
break
# if scrIdx>100:
# break
if scrIdx%100==99:
print("screen,cam ",scrIdx,camIdx)
print("{} of {} completed".format(scrIdx+1,len(screenTime)))
vidOut.release()
print("Finished writing to video {}".format(OUTPUT_VIDEO_FILE))
print("End of program")
import numpy as np
import cv2
import sys
print("python traverse-mp4-framebyframe.py video.mp4 0.5")
print("python traverse-mp4-framebyframe.py argv[1] [2]")
VIDEO_FILE=sys.argv[1]
RATIO=float(sys.argv[2])
vidIn=cv2.VideoCapture(VIDEO_FILE)
frIdx=0
ret,fr=vidIn.read()
while vidIn.isOpened():
noFramesToJump=int(input("Which frame to display? "))
vidIn.set(cv2.CAP_PROP_POS_FRAMES, noFramesToJump)
ret,fr=vidIn.read()
if not ret: break
fr=cv2.resize(fr,dsize=(int(fr.shape[1]*RATIO),int(fr.shape[0]*RATIO)))
print("Frame {}".format(frIdx))
cv2.imshow("Frame",fr)
cv2.waitKey(100)
# cv2.destroyAllWindows()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment