hydrus/include/HydrusVideoHandling.py

107 lines
2.7 KiB
Python
Raw Normal View History

2014-05-07 22:42:30 +00:00
#import numpy.core.multiarray # important this comes before cv!
import cv2
2013-02-19 00:11:43 +00:00
from flvlib import tags as flv_tags
2013-07-10 20:25:57 +00:00
import HydrusConstants as HC
2014-04-30 21:31:40 +00:00
import matroska
2013-07-10 20:25:57 +00:00
import os
2013-02-19 00:11:43 +00:00
import traceback
2013-08-07 22:25:18 +00:00
def GetFLVProperties( path ):
2013-02-19 00:11:43 +00:00
2013-08-14 20:21:49 +00:00
with open( path, 'rb' ) as f:
2013-02-19 00:11:43 +00:00
2013-08-07 22:25:18 +00:00
flv = flv_tags.FLV( f )
script_tag = None
for tag in flv.iter_tags():
2013-02-19 00:11:43 +00:00
2013-08-07 22:25:18 +00:00
if isinstance( tag, flv_tags.ScriptTag ):
script_tag = tag
break
2013-02-19 00:11:43 +00:00
2013-08-07 22:25:18 +00:00
width = 853
height = 480
duration = 0
num_frames = 0
2013-02-19 00:11:43 +00:00
2013-08-07 22:25:18 +00:00
if script_tag is not None:
tag_dict = script_tag.variable
# tag_dict can sometimes be a float?
# it is on the broken one I tried!
if 'width' in tag_dict: width = tag_dict[ 'width' ]
if 'height' in tag_dict: height = tag_dict[ 'height' ]
if 'duration' in tag_dict: duration = int( tag_dict[ 'duration' ] * 1000 )
if 'framerate' in tag_dict: num_frames = int( ( duration / 1000.0 ) * tag_dict[ 'framerate' ] )
2013-02-19 00:11:43 +00:00
2013-08-07 22:25:18 +00:00
return ( ( width, height ), duration, num_frames )
2013-02-19 00:11:43 +00:00
2013-07-10 20:25:57 +00:00
2013-08-14 20:21:49 +00:00
def GetCVVideoProperties( path ):
2013-07-10 20:25:57 +00:00
2014-05-07 22:42:30 +00:00
capture = cv2.VideoCapture( path )
2013-07-10 20:25:57 +00:00
2014-05-07 22:42:30 +00:00
num_frames = int( capture.get( cv2.cv.CV_CAP_PROP_FRAME_COUNT ) )
2013-07-10 20:25:57 +00:00
2014-05-07 22:42:30 +00:00
fps = capture.get( cv2.cv.CV_CAP_PROP_FPS )
2013-07-10 20:25:57 +00:00
2013-08-07 22:25:18 +00:00
length_in_seconds = num_frames / fps
2013-07-10 20:25:57 +00:00
length_in_ms = int( length_in_seconds * 1000 )
2013-08-14 20:21:49 +00:00
duration = length_in_ms
2013-08-07 22:25:18 +00:00
2014-05-07 22:42:30 +00:00
width = int( capture.get( cv2.cv.CV_CAP_PROP_FRAME_WIDTH ) )
2013-08-14 20:21:49 +00:00
2014-05-07 22:42:30 +00:00
height = int( capture.get( cv2.cv.CV_CAP_PROP_FRAME_HEIGHT ) )
2013-08-07 22:25:18 +00:00
return ( ( width, height ), duration, num_frames )
2014-04-30 21:31:40 +00:00
def GetMatroskaOrWebm( path ):
tags = matroska.parse( path )
ebml = tags[ 'EBML' ][0]
if ebml[ 'DocType' ][0] == 'matroska': return HC.VIDEO_MKV
elif ebml[ 'DocType' ][0] == 'webm': return HC.VIDEO_WEBM
raise Exception()
def GetMatroskaOrWebMProperties( path ):
tags = matroska.parse( path )
segment = tags['Segment'][0]
info = segment['Info'][0]
duration = int( ( info['Duration'][0] * info['TimecodeScale'][0] / 1e9 ) * 1000 )
tracks = segment['Tracks'][0]
trackentries = tracks['TrackEntry']
for trackentry in trackentries:
if 'Video' in trackentry:
video = trackentry['Video'][0]
width = video[ 'PixelWidth' ][0]
height = video[ 'PixelHeight' ][0]
break
num_frames = 0
return ( ( width, height ), duration, num_frames )
2013-07-10 20:25:57 +00:00