Видео и аудио кодирование

Материал из Artem Aleksashkin's Wiki
Перейти к навигации Перейти к поиску

Видео

Кодеки

H.264

function transform_video_x264_aac_mp4($file_src,$file_dst,$width,$height,$vb,$ab,$tmp_dir="./")
{
	if (!file_exists($file_src)) return false;
	$X264_SUBQ="1";
	$X264_FRAMEREF="1";
	$X264_KEYINT="125";
	$X264_THREADS="auto";
	$X264_ME="hex";
	$X264_BFRAMES="1";
	$session_id=time();
	$files_prefix = $tmp_dir."".$session_id;
	$mediainfo = $this->exec_program("mediainfo ".$file_src);
	preg_match("/General[\s\S]*?Format\s+\:\s+(.*)\s+/",$mediainfo,$m);
	$format = $m[1];
	$format_profile = "";
	if (preg_match("/General[\s\S]*?Format profile\s+\:\s+(.*)\s+/",$mediainfo,$m))
	{
		$format_profile = $m[1];
	}
	$format_info = "";
	if (preg_match("/General[\s\S]*?Format\/Info\s+\:\s+(.*)\s+/",$mediainfo,$m))
	{
		$format_info = $m[1];
	}
	preg_match("/Video[\s\S]*?Frame rate\s+\:\s+(.*?)\s+fps/",$mediainfo,$m);
	//echo $format."\n";
	$fps = $m[1];
	if ($format_info == "Apple")
	{
		// dump audio
		$this->exec_program("mplayer -quiet ".$file_src." -ao pcm:fast:file=".$files_prefix."_audiostream -vc dummy -vo null -channels 2");
		// encode video from src
		$this->exec_program("mencoder ".$file_src." -ovc x264 -x264encopts bitrate=".$vb.":subq=".$X264_SUBQ.":frameref=".$X264_FRAMEREF.":me=".$X264_ME.":bframes=".$X264_BFRAMES.":threads=".$X264_THREADS.":keyint=".$X264_KEYINT." -of rawvideo -vf scale=".$width.":".$height." -ofps ".$fps." -nosound -o ".$files_prefix."_videostream.x264");
		// encode audio
		$this->exec_program("ffmpeg -i ".$files_prefix."_audiostream -strict experimental -acodec aac -ab ".$ab." -ac 2 ".$files_prefix."_audiostream.aac");
		$this->exec_program("MP4Box -new -add ".$files_prefix."_videostream.x264 -fps ".$fps." -add ".$files_prefix."_audiostream.aac ".$file_dst."");
		$this->exec_program("rm -f ".$files_prefix."_*");
		return true;
	}
	if ($format_profile == "QuickTime")
	{
		// dump audio
		$this->exec_program("mplayer -quiet ".$file_src." -ao pcm:fast:file=".$files_prefix."_audiostream -vc dummy -vo null -channels 2");
		// encode video from src
		$this->exec_program("mencoder ".$file_src." -ovc x264 -x264encopts bitrate=".$vb.":subq=".$X264_SUBQ.":frameref=".$X264_FRAMEREF.":me=".$X264_ME.":bframes=".$X264_BFRAMES.":threads=".$X264_THREADS.":keyint=".$X264_KEYINT." -of rawvideo -vf scale=".$width.":".$height." -ofps ".$fps." -nosound -o ".$files_prefix."_videostream.x264");
		// encode audio
		$this->exec_program("ffmpeg -i ".$files_prefix."_audiostream -strict experimental -acodec aac -ab ".$ab." -ac 2 ".$files_prefix."_audiostream.aac");
		$this->exec_program("MP4Box -new -add ".$files_prefix."_videostream.x264 -fps ".$fps." -add ".$files_prefix."_audiostream.aac ".$file_dst."");
		$this->exec_program("rm -f ".$files_prefix."_*");
		return true;
	}
	if ($format == "MPEG-4")
	{
		$mpeg4tracks = $this->exec_program("MP4Box -info ".$file_src);
		$tracks = preg_split("/\n\n/",$mpeg4tracks);
		$videotrack = false;
		foreach($tracks as $track)
		{
			if (preg_match("/Track # (\d+)[\s\S]*?Visual Stream/",$track,$m))
			{
				$videotrack = $m[1];
			}
		}
		$audiotrack = false;
		foreach($tracks as $track)
		{
			if (preg_match("/Track # (\d+)[\s\S]*?Audio (Stream|Track)/",$track,$m))
			{
				$audiotrack = $m[1];
			}
		}
		if ($videotrack === false) return false;
		$this->exec_program("MP4Box -raw ".$videotrack." ".$file_src." -out ".$files_prefix."_videostream");
		if ($audiotrack !== false) $this->exec_program("MP4Box -raw ".$audiotrack." ".$file_src." -out ".$files_prefix."_audiostream");
		$this->exec_program("mencoder ".$files_prefix."_videostream -ovc x264 -x264encopts bitrate=".$vb.":subq=".$X264_SUBQ.":frameref=".$X264_FRAMEREF.":me=".$X264_ME.":bframes=".$X264_BFRAMES.":threads=".$X264_THREADS.":keyint=".$X264_KEYINT." -of rawvideo -vf scale=".$width.":".$height." -ofps ".$fps." -nosound -o ".$files_prefix."_videostream.x264");
		if ($audiotrack !== false) $this->exec_program("ffmpeg -i ".$files_prefix."_audiostream -strict experimental -acodec aac -ab ".$ab." -ac 2 ".$files_prefix."_audiostream.aac");
		if ($audiotrack !== false) $this->exec_program("MP4Box -new -add ".$files_prefix."_videostream.x264 -fps ".$fps." -add ".$files_prefix."_audiostream.aac ".$file_dst."");
		else $this->exec_program("MP4Box -new -add ".$files_prefix."_videostream.x264 -fps ".$fps." ".$file_dst."");
		$this->exec_program("rm -f ".$files_prefix."_*");
		return true;
	}
	if ($format == "AVI")//Format Audio Video Interleave
	{
		// dump audio
		$this->exec_program("mplayer -quiet ".$file_src." -ao pcm:fast:file=".$files_prefix."_audiostream -vc dummy -vo null -channels 2");
		// encode video from src
		$this->exec_program("mencoder ".$file_src." -ovc x264 -x264encopts bitrate=".$vb.":subq=".$X264_SUBQ.":frameref=".$X264_FRAMEREF.":me=".$X264_ME.":bframes=".$X264_BFRAMES.":threads=".$X264_THREADS.":keyint=".$X264_KEYINT." -of rawvideo -vf scale=".$width.":".$height." -ofps ".$fps." -nosound -o ".$files_prefix."_videostream.x264");
		// encode audio
		$this->exec_program("ffmpeg -i ".$files_prefix."_audiostream -strict experimental -acodec aac -ab ".$ab." -ac 2 ".$files_prefix."_audiostream.aac");
		$this->exec_program("MP4Box -new -add ".$files_prefix."_videostream.x264 -fps ".$fps." -add ".$files_prefix."_audiostream.aac ".$file_dst."");
		$this->exec_program("rm -f ".$files_prefix."_*");
		return true;
	}
	if ($format == "Windows Media")//WMV
	{
		// dump audio
		$this->exec_program("mplayer -quiet ".$file_src." -ao pcm:fast:file=".$files_prefix."_audiostream -vc dummy -vo null -channels 2");
		// encode video from src
		$this->exec_program("mencoder ".$file_src." -ovc x264 -x264encopts bitrate=".$vb.":subq=".$X264_SUBQ.":frameref=".$X264_FRAMEREF.":me=".$X264_ME.":bframes=".$X264_BFRAMES.":threads=".$X264_THREADS.":keyint=".$X264_KEYINT." -of rawvideo -vf scale=".$width.":".$height." -ofps ".$fps." -nosound -o ".$files_prefix."_videostream.x264");
		// encode audio
		$this->exec_program("ffmpeg -i ".$files_prefix."_audiostream -strict experimental -acodec aac -ab ".$ab." -ac 2 ".$files_prefix."_audiostream.aac");
		$this->exec_program("MP4Box -new -add ".$files_prefix."_videostream.x264 -fps ".$fps." -add ".$files_prefix."_audiostream.aac ".$file_dst."");
		$this->exec_program("rm -f ".$files_prefix."_*");
		return true;
	}
	if ($format == "Flash Video")//FLV
	{
		// dump audio
		$this->exec_program("mplayer -quiet ".$file_src." -ao pcm:fast:file=".$files_prefix."_audiostream -vc dummy -vo null -channels 2");
		// encode video from src
		$this->exec_program("mencoder ".$file_src." -ovc x264 -x264encopts bitrate=".$vb.":subq=".$X264_SUBQ.":frameref=".$X264_FRAMEREF.":me=".$X264_ME.":bframes=".$X264_BFRAMES.":threads=".$X264_THREADS.":keyint=".$X264_KEYINT." -of rawvideo -vf scale=".$width.":".$height." -ofps ".$fps." -nosound -o ".$files_prefix."_videostream.x264");
		// encode audio
		$this->exec_program("ffmpeg -i ".$files_prefix."_audiostream -strict experimental -acodec aac -ab ".$ab." -ac 2 ".$files_prefix."_audiostream.aac");
		$this->exec_program("MP4Box -new -add ".$files_prefix."_videostream.x264 -fps ".$fps." -add ".$files_prefix."_audiostream.aac ".$file_dst."");
		$this->exec_program("rm -f ".$files_prefix."_*");
		return true;
	}
	return false;
}

VP9

Контейнеры