hydrus/hydrus/core/HydrusFlashHandling.py

64 lines
1.6 KiB
Python
Raw Normal View History

2020-04-22 21:00:35 +00:00
from hydrus.external import hexagonitswfheader
from hydrus.core import HydrusConstants as HC
from hydrus.core import HydrusData
2015-11-25 22:00:57 +00:00
import os
import subprocess
2016-03-30 22:56:50 +00:00
import time
2013-02-19 00:11:43 +00:00
import traceback
2015-11-25 22:00:57 +00:00
if HC.PLATFORM_LINUX:
SWFRENDER_PATH = os.path.join( HC.BIN_DIR, 'swfrender_linux' )
2019-11-20 23:10:46 +00:00
elif HC.PLATFORM_MACOS:
2015-11-25 22:00:57 +00:00
SWFRENDER_PATH = os.path.join( HC.BIN_DIR, 'swfrender_osx' )
elif HC.PLATFORM_WINDOWS:
SWFRENDER_PATH = os.path.join( HC.BIN_DIR, 'swfrender_win32.exe' )
2013-02-19 00:11:43 +00:00
# to all out there who write libraries:
# hexagonit.swfheader is a perfect library. it is how you are supposed to do it.
2013-08-07 22:25:18 +00:00
def GetFlashProperties( path ):
2013-02-19 00:11:43 +00:00
2013-08-14 20:21:49 +00:00
with open( path, 'rb' ) as f:
2013-08-07 22:25:18 +00:00
metadata = hexagonitswfheader.parse( f )
width = metadata[ 'width' ]
height = metadata[ 'height' ]
num_frames = metadata[ 'frames' ]
fps = metadata[ 'fps' ]
2019-01-09 22:59:03 +00:00
duration = ( 1000 * num_frames ) // fps
2013-08-07 22:25:18 +00:00
return ( ( width, height ), duration, num_frames )
2015-11-25 22:00:57 +00:00
def RenderPageToFile( path, temp_path, page_index ):
2019-01-09 22:59:03 +00:00
cmd = [ SWFRENDER_PATH, path, '-o', temp_path, '-p', str( page_index ) ]
2015-11-25 22:00:57 +00:00
2016-03-30 22:56:50 +00:00
timeout = HydrusData.GetNow() + 60
2019-01-09 22:59:03 +00:00
sbp_kwargs = HydrusData.GetSubprocessKWArgs()
p = subprocess.Popen( cmd, **sbp_kwargs )
2015-11-25 22:00:57 +00:00
2016-03-30 22:56:50 +00:00
while p.poll() is None:
if HydrusData.TimeHasPassed( timeout ):
p.terminate()
raise Exception( 'Could not render the swf page within 60 seconds!' )
time.sleep( 0.5 )
2015-11-25 22:00:57 +00:00
p.communicate()
2017-06-07 22:05:15 +00:00