luckymage
5/9/2017 - 2:13 AM

translate the array data of time-filename from bookmark.txt,translate into many .kva file by filename

translate the array data of time-filename from bookmark.txt,translate into many .kva file by filename

<?php 
/**
 * translate the array data of time-filename from bookmark.txt,translate into many .kva file by filename
 */
class BookmarkToXMLFILE{
	private $keyframesXML_dom;
	private $folderNmae;
	public $keyframeRate;
	public function __construct(){
		//$this->keyframesXML_dom=$this->keyframesXMLDOM("src/keyframe_templae.xml");
		$this->folderNmae='output';
	}
	/**
	 * get the array which classfy by filename,and the xml string from <keyframe>;get the filename and time data from array in sequence,and write them into xml string.Finally,combine as xml string which include time data.;Finally,let's the xml string combine with the .kva template.According filename translating into .kva file
	 * @param type $diff_bookmark_arr
	 * @param type $keyframe_xml_str
	 */
	public function arrToXML($diff_bookmark_arr,$keyframe_xml_str){
		foreach($diff_bookmark_arr as $i=>$bookmark_arr){
			$fileName=$bookmark_arr['fileName'];			
			$time_arr=$bookmark_arr['time_arr'];
			foreach($time_arr as $j=>$time_str){
				
				
				$tmp=simplexml_load_string($keyframe_xml_str);
				$tmp->Position->attributes()->UserTime='0:'.$time_str;
				$tmp->Position=$this->timeToKeyframeNum($time_str);
				$tmp->Title='0:'.$time_str;
				$keyframe_arr[]=$tmp->asXML();

			}
			$keyframe_str='<Keyframes>'.implode($keyframe_arr).'</Keyframes>';
			
			unset($keyframe_arr);//koooo
			$xmlHeader="<\?xml version=\"1.0\"\?>";
			$keyframe_str=preg_replace("/$xmlHeader/", '', $keyframe_str);

			//print_r($keyframesXML_dom->saveXML());
			$this->mergeKeyframXMLToFile($keyframe_str,$fileName);
		}

	}
	/**
	 * get time format data, explode() the unit by minute and second.By video file's keyframeRate and the <AverageTimeStampsPerFrame> value from.kva,to count the time format for .kva
	 * @param type $str
	 * @return type
	 */
	private function timeToKeyframeNum($str){
		$keyframeRate=$this->keyframeRate;//Todo-should change to get KeyframeRate directly from movie file,never manual keyin by hand
		$sum=0;
		$loc=strpos($str,':');
		$loc=$loc+1;
		$str=substr($str, $loc);
		$arr=explode(':', $str);
		$arr=array_reverse($arr);
		// $AverageTimeStampsPerFrame=2;
		$AverageTimeStampsPerFrame=20;//Todo-should get from keyframe_templae.xml
		// print_r($arr);
		foreach($arr as $i=>$value){
			if($i<1){
				$sum+=(int)$value*$keyframeRate*$AverageTimeStampsPerFrame;
			}else{
				$sum+=(int)$value*$keyframeRate*$AverageTimeStampsPerFrame*60;
			}

		}


		
		/*foreach($arr as $i=>$value){
			if($i<1){
				$sum+=(int)$value*$keyframeRate*100;
			}else{
				$sum+=(int)$value*$keyframeRate*100*60;
			}

		}*/
		
		/*foreach($arr as $i=>$value){
			

			// if($i<1){
			// 	$sum+=(int)$value*1000;
			// }else{
			// 	$sum+=(int)$value*1000*60;
			// }

			if($i<1){
				$sum+=(int)$value*15;
			}else{
				$sum+=(int)$value*15*60;
			}

		}
*/


		return $sum;	
	}
	/**
	 * get the kva time format string-"$keyframe_str", ready to combine with .kva template.After combining, save as .kva file by filename.
	 * @param type $keyframe_str
	 * @param type $fileName
	 * @param type $keyframesXML_dom
	 */
	private function mergeKeyframXMLToFile($keyframe_str,$fileName){
		//
		$keyframesXML_dom=$this->keyframesXMLDOM("src/keyframe_templae.xml");
		$sub=$keyframesXML_dom->getElementsByTagName('OriginalFilename');
		$sub->item(0)->nodeValue=$fileName;
		//
		$f=$keyframesXML_dom->createDocumentFragment();//todo-it's too messy below
		
		$f->appendXML($keyframe_str);//-error
		//print_r($keyframe_str);
		$keyframesXML_dom->documentElement->appendChild($f);
		//print($keyframesXML_dom->documentElement);
		//$keyframesXML_dom->saveXML('outttt.xml');
		$fileName=$this->changeFilenameExtension($fileName);
		print_r($fileName);
		$fileName=iconv('', 'utf-8', $fileName);
		
		$fileName=trim($fileName);
		if(!is_dir($this->folderNmae)){
			mkdir($this->folderNmae);

		}
		
		$keyframesXML_dom->save($this->folderNmae.'/'.$fileName);//-problem
		unset($keyframesXML_dom);
		
		//$keyframesXML_dom->save('output/'.$fileName);//-problem
		//$keyframesXML_dom->save('a.xml');
	}
	/**
	 * translate string into xml format, and encode as utf-8
	 * @param type $xmlfileLoc_str
	 * @return \DOMDocument
	 */
	private function keyframesXMLDOM($xmlfileLoc_str){
		$dom=new DOMDocument('1.0');
		$dom->encoding='UTF-8';
		$dom->load($xmlfileLoc_str);
		
		return $dom;
	}
	/**
	 * change filename extenstion from .mp4->.xml
	 * @param  [type] $str
	 * @return [type] $str
	 */
	private function changeFilenameExtension($str){
		$arr=explode('\\', $str);
		$arr=array_reverse($arr);
		$str=$arr[0];
		//$str=str_replace('.mp4]','.xml',$str);
		$patterns = array();
		$patterns[0] = '/.mp4]/';//Todo-should list all movie format extension(mp4,mov,wmv) as string , and then translate them as array ,never write so many individual array .
		$patterns[1] = '/.mov]/';//Todo-why use "]"
		$patterns[2] = '/.wmv]/';
		//$str=preg_replace($patterns,'.xml',$str);
		$str=preg_replace($patterns,'.kva',$str);

		return $str;
	}

}
/*=============================
=            index.php            =
=============================*/


?>