#!/usr/bin/env python

import bootstrap
import sys
import openpluginlib
import openimagelib
import openmedialib
import common

openpluginlib.init( "" )

input_args = []
store_args = []

sys.argv.pop( 0 )

for i in sys.argv:
	if len( input_args ) == 0 or ( len( store_args ) == 0 and i.find( '=' ) > 0 ):
		input_args.append( i )
	else:
		store_args.append( i )

if len( input_args ) > 0 and len( store_args ) > 0:
	input = common.input_wrapper( openmedialib.create_input( input_args[ 0 ] ) )
	if input.valid( ):
		input_name = input_args.pop( 0 )
		input.init( input_args )
		frame = input.fetch( )
		if frame is not None:
			store = openmedialib.create_store( store_args[ 0 ], frame )
			if store is not None:
				store_name = store_args.pop( 0 )
				common.assign_props( store.properties( ), store_args )
				if store_name != "pipe:":
					common.report( "Input " + input_name, input.properties( ) )
					common.report( "Store " + store_name, store.properties( ) )
				if store.init( ):
					try:
						i = 0
						while i < input.get_frames( ):
							if not store.push( frame ):
								print "failed to push frame"
								break
							i += 1
							input.seek( i, False )
							frame = input.fetch( )
					except KeyboardInterrupt:
						pass
					store.complete( )
				else:
					print "failed to init store"
			else:
				print "failed to open store"
		else:
			print "failed to fetch frame"
	else:
		print "failed to open input"
	
openpluginlib.uninit( )
