<commit_before><?php
/**
 * @package server-infra
 * @subpackage Media
 */
class KMediaInfoMediaParser extends KBaseMediaParser 
{
	protected $cmdPath;
	
	const SrteamGeneral = "general";
	const SrteamVideo = "video";
	const SrteamAudio = "audio";
	const SrteamImage = "image";
	
	/**
	 * @param string $filePath
	 * @param string $cmdPath
	 */
	public function __construct($filePath, $cmdPath)
	{
		$this->cmdPath = $cmdPath;
		parent::__construct($filePath);
	}
	
	/**
	 * @return KalturaMediaInfo
	 */
	public function getMediaInfo()
	{
		$output = $this->getRawMediaInfo();
		$kMi = $this->parseOutput($output);

		if(!isset($kMi)) {
			return null;
		}
			/*
			 * Following code patches mediainfo 0.7.61 misbehaviours, 
			 * those behaviors do not appear on the older 0.7.28.
			 */
		{
			 /*
			 * Interlaced mjpa sources - the height value is halved.
			 */
			if(isset($kMi->videoHeightTmp) 
			&& isset($kMi->videoCodecId) && $kMi->videoCodecId=="mjpa"
			&& isset($kMi->scanType) && $kMi->scanType==1){
				$kMi->videoHeight = $kMi->videoHeightTmp;
			}
			/*
			 * WebM/VP8 misses video duration
			 */
			if(isset($kMi->videoFormat) && $kMi->videoFormat=="vp8" // isset($kMi->videoCodecId) && $kMi->videoCodecId=="v_vp8"
			&& (!isset($kMi->videoDuration) || $kMi->videoDuration==0)){
				$kMi->videoDuration = $kMi->containerDuration;
			}
		}
		
		$durLimit=3600000;
		if(get_class($this)=='KMediaInfoMediaParser'
		&& ((isset($kMi->containerDuration) && $kMi->containerDuration>=$durLimit) 
			|| (isset($kMi->videoDuration) && $kMi->videoDuration>=$durLimit)
			|| (isset($kMi->audioDuration) && $kMi->audioDuration>=$durLimit))) {
			$cmd = "{$this->cmdPath} \"--Inform=General;done %Duration%\" \"{$this->filePath}\"";
			$output=0;
			$output = shell_exec($cmd);
			$aux = explode(" ", $output);
			if(isset($aux) && count($aux)==2 && $aux[0]=='done'){
				$kMi->containerDuration=(int)$aux[1];
			}
			$cmd = "{$this->cmdPath} \"--Inform=Video;done %Duration%\" \"{$this->filePath}\"";
			$output=0;
			$output = shell_exec($cmd);
			$aux = explode(" ", $output);
			if(isset($aux) && count($aux)==2 && $aux[0]=='done'){
				$kMi->videoDuration=(int)$aux[1];
			}
			$cmd = "{$this->cmdPath} \"--Inform=Audio;done %Duration%\" \"{$this->filePath}\"";
			$output=0;
			$output = shell_exec($cmd);
			$aux = explode(" ", $output);
			if(isset($aux) && count($aux)==2 && $aux[0]=='done'){
				$kMi->audioDuration=(int)$aux[1];
			}
		}
		return $kMi;
	}
	
	protected function getCommand() 
	{
		return "{$this->cmdPath} \"{$this->filePath}\"";
	}
	
	protected function parseOutput($output) 
	{
		$output = kXml::stripXMLInvalidChars($output);
		$tokenizer = new KStringTokenizer ( $output, "\t\n" );
		$mediaInfo = new KalturaMediaInfo();
		$mediaInfo->rawData = $output;
		
		$fieldCnt = 0;<commit_msg>Fixed duration processing of long (>1hr) files. To solve failure of Webm/VP8 assets<commit_after><?php
/**
 * @package server-infra
 * @subpackage Media
 */
class KMediaInfoMediaParser extends KBaseMediaParser 
{
	protected $cmdPath;
	
	const SrteamGeneral = "general";
	const SrteamVideo = "video";
	const SrteamAudio = "audio";
	const SrteamImage = "image";
	
	/**
	 * @param string $filePath
	 * @param string $cmdPath
	 */
	public function __construct($filePath, $cmdPath)
	{
		$this->cmdPath = $cmdPath;
		parent::__construct($filePath);
	}
	
	/**
	 * @return KalturaMediaInfo
	 */
	public function getMediaInfo()
	{
		$output = $this->getRawMediaInfo();
		$kMi = $this->parseOutput($output);

		if(!isset($kMi)) {
			return null;
		}
			/*
			 * Following code patches mediainfo 0.7.61 misbehaviours, 
			 * those behaviors do not appear on the older 0.7.28.
			 */
		{
			 /*
			 * Interlaced mjpa sources - the height value is halved.
			 */
			if(isset($kMi->videoHeightTmp) 
			&& isset($kMi->videoCodecId) && $kMi->videoCodecId=="mjpa"
			&& isset($kMi->scanType) && $kMi->scanType==1){
				$kMi->videoHeight = $kMi->videoHeightTmp;
			}
			/*
			 * WebM/VP8 misses video duration
			 */
			if(isset($kMi->videoFormat) && $kMi->videoFormat=="vp8" // isset($kMi->videoCodecId) && $kMi->videoCodecId=="v_vp8"
			&& (!isset($kMi->videoDuration) || $kMi->videoDuration==0)){
				$kMi->videoDuration = $kMi->containerDuration;
			}
		}
		
		$durLimit=3600000;
		if(get_class($this)=='KMediaInfoMediaParser'
		&& ((isset($kMi->containerDuration) && $kMi->containerDuration>=$durLimit) 
			|| (isset($kMi->videoDuration) && $kMi->videoDuration>=$durLimit)
			|| (isset($kMi->audioDuration) && $kMi->audioDuration>=$durLimit))) {
			$cmd = "{$this->cmdPath} \"--Inform=General;done %Duration%\" \"{$this->filePath}\"";
			$output=0;
			$output = shell_exec($cmd);
			$aux = explode(" ", trim($output));
			if(isset($aux) && count($aux)==2 && $aux[0]=='done'){
				$kMi->containerDuration=(int)$aux[1];
			}
			$cmd = "{$this->cmdPath} \"--Inform=Video;done %Duration%\" \"{$this->filePath}\"";
			$output=0;
			$output = shell_exec($cmd);
			$aux = explode(" ", trim($output));
			if(isset($aux) && count($aux)==2 && $aux[0]=='done'){
				$kMi->videoDuration=(int)$aux[1];
			}
			$cmd = "{$this->cmdPath} \"--Inform=Audio;done %Duration%\" \"{$this->filePath}\"";
			$output=0;
			$output = shell_exec($cmd);
			$aux = explode(" ", trim($output));
			if(isset($aux) && count($aux)==2 && $aux[0]=='done'){
				$kMi->audioDuration=(int)$aux[1];
			}
		}
		return $kMi;
	}
	
	protected function getCommand() 
	{
		return "{$this->cmdPath} \"{$this->filePath}\"";
	}
	
	protected function parseOutput($output) 
	{
		$output = kXml::stripXMLInvalidChars($output);
		$tokenizer = new KStringTokenizer ( $output, "\t\n" );
		$mediaInfo = new KalturaMediaInfo();
		$mediaInfo->rawData = $output;
		
		$fieldCnt = 0;
{'fraction_non_alphanumeric': 0.211545169166376, 'fraction_numerical': 0.011859086152772933, 'mean_word_length': 3.9482312338222605, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 82, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7316165', 'n_tokens_mistral': 2443, 'n_tokens_neox': 2082, 'n_words': 347}