-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
77 lines (73 loc) · 3.62 KB
/
__init__.py
File metadata and controls
77 lines (73 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import logging
import cv2
import math
from datetime import datetime
import json
import tempfile
from azure.storage.blob import BlockBlobService
import sys
import os
sys.path.append(os.path.abspath('.'))
import MyClasses
import azure.functions as func
def main(req: func.HttpRequest) -> func.HttpResponse:
## Get blob details
fileURL = "https://fsevideos.blob.core.windows.net/us-office/Brighton Highlights.mp4"
container = "us-office"
fileName = "Brighton Highlights.mp4"
timeToCutStr = "2095-03-13 00:00:00.00000"
timeToCut = datetime.strptime(timeToCutStr,
"%Y-%m-%d %H:%M:%S.%f")
logging.info(f"fileURL: {fileURL}")
logging.info(f"container: {container}")
logging.info(f"fileName: {fileName}")
logging.info(f"timeToCutStr: {timeToCutStr}")
## Open the video
vidcap = cv2.VideoCapture(fileURL)
logging.info(f"VideoCapture object created for {fileURL}")
success,image = vidcap.read()
## Get metadata
fps = vidcap.get(cv2.CAP_PROP_FPS)
frameCount = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
logging.info('Video metadata acquired')
logging.info(f"frameCount: {str(frameCount)}")
## If frame count negative, download locally and try again
if frameCount <= 0:
logging.info("Frame count greater than 0, so local download needed")
with tempfile.TemporaryDirectory() as dirpath:
## Get blob and save to local directory
vidLocalPath = fr"{dirpath}\{fileName}"
# logging.info("About to get connection string")
# logging.info(f"CS: {os.environ['fsevideosConnectionString']}")
fsevideosConnectionString = "DefaultEndpointsProtocol=https;AccountName=fsevideos;AccountKey=xfYncTDRCowSrISbdsSknM05jqOrJXc4Oavq7BQ56yR7uQ7MCeL5aXmBsbsE+SZ+++xGt2oy6FvrEdpryc+vwQ==;EndpointSuffix=core.windows.net"
logging.info("About to create BlockBlobService")
block_blob_service = BlockBlobService(connection_string=fsevideosConnectionString)
logging.info("BlockBlobService created")
block_blob_service.get_blob_to_path(container_name=container,
blob_name=fileName,
file_path=vidLocalPath)
logging.info("Blob saved to path")
with MyClasses.MyVideoCapture(vidLocalPath) as vc1:
frameCount = int(vc1.get(cv2.CAP_PROP_FRAME_COUNT))
logging.info(f"(new) frameCount: {str(frameCount)}")
## Get number of frames wanted per second
wantedFPS = 1
takeEveryN = math.floor(fps/wantedFPS)
if timeToCutStr != "2095-03-13 00:00:00.00000":
## Work out when the recording starts based on the filename
vidName = fileName.split("\\")[-1].replace(".mp4","")
vidName1 = vidName[:vidName.index("-")]
recordingStart = datetime.strptime(f'{vidName1.split("_")[0]} {vidName1[-4:]}',
"%Y%m%d %H%M")
## Work out which frames to reject
frameToCutFrom = int((timeToCut - recordingStart).seconds * fps)
else:
## If last play is my 100th birthday, set a huge number that it'll never reach
frameToCutFrom = 1000000000
logging.info("List of frame numbers about to be generated")
## Create list of frame numbers to be JPEGed
listOfFrameNumbers = [i
for i in range(frameCount)
if (i % takeEveryN == 0) & (i <= frameToCutFrom)]
logging.info(f"listOfFrameNumbers created with {len(listOfFrameNumbers)} elements")
return func.HttpResponse(json.dumps(listOfFrameNumbers))