php+ffmpeg如何獲取視頻縮略圖、視頻分辨率等相關(guān)信息
前言
ffmpeg是一款開(kāi)源、跨平臺(tái)的視頻處理程序,可用在Windows、mac、linux等平臺(tái),可以方便的運(yùn)用多種語(yǔ)言腳本來(lái)調(diào)用其執(zhí)行視頻的操作。
下面介紹使用ffmpeg獲取視頻首幀的方法。
<?php //待處理視頻 $in_file = 'http://cdn.fintechuni.cn/video/2019/12/25/222834_f93690f37496456a9674763d69bcd9c0.mp4'; //縮略圖保存路徑 $out_file = './test.jpg'; //shell腳本 $shell = "ffmpeg -i $in_file -y -f image2 -ss 00:00:01 -vframes 1 $out_file 2>&1"; //調(diào)用php的exec方法去執(zhí)行腳本 exec($shell, $output, $return_val); //獲取輸出信息 print_r($output);
FFmpeg獲得視頻文件的縮略圖
function getVideoCover($file,$time,$name) { if(empty($time))$time = '1';//默認(rèn)截取第一秒第一幀 $strlen = strlen($file); //exec("ffmpeg -i ".$file." -y -f mjpeg -ss ".$time." -t 0.001 -s 320x240 ".$name."",$out,$status); $str = "ffmpeg -i ".$file." -y -f mjpeg -ss 3 -t ".$time." -s 320x240 ".$name; $result = system($str); }
Fmpeg讀取視頻信息
<?php define('FFMPEG_PATH', '/usr/local/ffmpeg2/bin/ffmpeg -i "%s" 2>&1'); function getVideoInfo($file) { $command = sprintf(FFMPEG_PATH, $file); ob_start(); passthru($command); $info = ob_get_contents(); ob_end_clean(); $data = array(); if (preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/", $info, $match)) { $data['duration'] = $match[1]; //播放時(shí)間 $arr_duration = explode(':', $match[1]); $data['seconds'] = $arr_duration[0] * 3600 + $arr_duration[1] * 60 + $arr_duration[2]; //轉(zhuǎn)換播放時(shí)間為秒數(shù) $data['start'] = $match[2]; //開(kāi)始時(shí)間 $data['bitrate'] = $match[3]; //碼率(kb) } if (preg_match("/Video: (.*?), (.*?), (.*?)[,\s]/", $info, $match)) { $data['vcodec'] = $match[1]; //視頻編碼格式 $data['vformat'] = $match[2]; //視頻格式 $data['resolution'] = $match[3]; //視頻分辨率 $arr_resolution = explode('x', $match[3]); $data['width'] = $arr_resolution[0]; $data['height'] = $arr_resolution[1]; } if (preg_match("/Audio: (\w*), (\d*) Hz/", $info, $match)) { $data['acodec'] = $match[1]; //音頻編碼 $data['asamplerate'] = $match[2]; //音頻采樣頻率 } if (isset($data['seconds']) && isset($data['start'])) { $data['play_time'] = $data['seconds'] + $data['start']; //實(shí)際播放時(shí)間 } $data['size'] = filesize($file); //文件大小 return $data; } //用法 $video_info = getVideoInfo('video.mp4'); print_r($video_info); ?>
Fmpeg獲得視頻文件的總長(zhǎng)度時(shí)間和創(chuàng)建時(shí)間
function getTime($file){ $vtime = exec("ffmpeg -i ".$file." 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//");//總長(zhǎng)度 $ctime = date("Y-m-d H:i:s",filectime($file));//創(chuàng)建時(shí)間 //$duration = explode(":",$time); // $duration_in_seconds = $duration[0]*3600 + $duration[1]*60+ round($duration[2]);//轉(zhuǎn)化為秒 return array('vtime'=>$vtime, 'ctime'=>$ctime ); }
另外一種方法
ffprobe -v quiet -print_format json -show_format -show_streams test.mp4
結(jié)果
{
"streams": [
{
"index": 0,
"codec_name": "h264",
"codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
"profile": "High",
"codec_type": "video",
"codec_time_base": "1/1200",
"codec_tag_string": "avc1",
"codec_tag": "0x31637661",
"width": 538,
"height": 888,
"coded_width": 544,
"coded_height": 896,
"has_b_frames": 0,
"sample_aspect_ratio": "0:1",
"display_aspect_ratio": "0:1",
"pix_fmt": "yuv420p",
"level": 31,
"color_range": "tv",
"color_space": "bt709",
"color_transfer": "bt709",
"color_primaries": "bt709",
"chroma_location": "left",
"refs": 2,
"is_avc": "1",
"nal_length_size": "4",
"r_frame_rate": "30/1",
"avg_frame_rate": "30/1",
"time_base": "1/600",
"start_pts": 0,
"start_time": "0.000000",
"duration_ts": 6040,
"duration": "10.066667",
"bit_rate": "1022789",
"bits_per_raw_sample": "8",
"nb_frames": "302",
"disposition": {
"default": 1,
"dub": 0,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0
},
"tags": {
"creation_time": "2020-01-01 15:59:27",
"language": "und",
"handler_name": "Core Media Video"
}
},
{
"index": 1,
"codec_name": "aac",
"codec_long_name": "AAC (Advanced Audio Coding)",
"profile": "LC",
"codec_type": "audio",
"codec_time_base": "1/44100",
"codec_tag_string": "mp4a",
"codec_tag": "0x6134706d",
"sample_fmt": "fltp",
"sample_rate": "44100",
"channels": 1,
"channel_layout": "mono",
"bits_per_sample": 0,
"r_frame_rate": "0/0",
"avg_frame_rate": "0/0",
"time_base": "1/44100",
"start_pts": -2112,
"start_time": "-0.047891",
"duration_ts": 442368,
"duration": "10.031020",
"bit_rate": "45569",
"max_bit_rate": "48000",
"nb_frames": "432",
"disposition": {
"default": 1,
"dub": 0,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0
},
"tags": {
"creation_time": "2020-01-01 15:59:27",
"language": "und",
"handler_name": "Core Media Audio"
}
}
],
"format": {
"filename": "test.mp4",
"nb_streams": 2,
"nb_programs": 0,
"format_name": "mov,mp4,m4a,3gp,3g2,mj2",
"format_long_name": "QuickTime / MOV",
"start_time": "-0.047891",
"duration": "10.066667",
"size": "1351439",
"bit_rate": "1073991",
"probe_score": 100,
"tags": {
"major_brand": "mp42",
"minor_version": "1",
"compatible_brands": "isommp41mp42",
"creation_time": "2020-01-01 15:59:27"
}
}
}
總結(jié)
到此這篇關(guān)于php+ffmpeg如何獲取視頻縮略圖、視頻分辨率等相關(guān)信息的文章就介紹到這了,更多相關(guān)php+ffmpeg獲取視頻縮略圖、分辨率內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- PHP中使用FFMPEG獲取視頻縮略圖和視頻總時(shí)長(zhǎng)實(shí)例
- PHP使用FFmpeg獲取視頻播放總時(shí)長(zhǎng)與碼率等信息
- 利用Ffmpeg獲得flv視頻縮略圖和視頻時(shí)間的代碼
- php使用FFmpeg接口獲取視頻的播放時(shí)長(zhǎng)、碼率、縮略圖以及創(chuàng)建時(shí)間
- php 調(diào)用ffmpeg獲取視頻信息的簡(jiǎn)單實(shí)現(xiàn)
- php使用ffmpeg獲取視頻信息并截圖的實(shí)現(xiàn)方法
- PHP基于ffmpeg實(shí)現(xiàn)轉(zhuǎn)換視頻,截圖及生成縮略圖的方法
- php利用ffmpeg提取視頻中音頻與視頻畫面的方法詳解
- php使用ffmpeg向視頻中添加文字字幕的實(shí)現(xiàn)方法
相關(guān)文章
Laravel解決nesting level錯(cuò)誤和隱藏index.php的問(wèn)題
今天小編就為大家分享一篇Laravel解決nesting level錯(cuò)誤和隱藏index.php的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10laravel請(qǐng)求參數(shù)校驗(yàn)方法
今天小編就為大家分享一篇laravel請(qǐng)求參數(shù)校驗(yàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10php不使用插件導(dǎo)出excel的簡(jiǎn)單方法
這篇文章主要介紹了php不使用插件導(dǎo)出excel的簡(jiǎn)單方法,首先獲取需要導(dǎo)出的數(shù)據(jù)的數(shù)組,數(shù)組的格式在下面。之后就是定義文件名稱和需要導(dǎo)出的excel的樣式,最后就是循環(huán)數(shù)組,輸出數(shù)據(jù)了2014-03-03Yii2框架dropDownList下拉菜單用法實(shí)例分析
這篇文章主要介紹了Yii2框架dropDownList下拉菜單用法,結(jié)合實(shí)例形式分析了Yii框架中dropDownList下拉菜單的定義、實(shí)現(xiàn)方法與使用技巧,需要的朋友可以參考下2016-07-07在Mac OS上搭建Nginx+PHP+MySQL開(kāi)發(fā)環(huán)境的教程
這篇文章主要介紹了在Mac OS上安裝配置Nginx+PHP+MySQL開(kāi)發(fā)環(huán)境的教程,雖然Mac自帶PHP,但還是要注意一下php-fpm報(bào)錯(cuò)問(wèn)題的發(fā)生,需要的朋友可以參考下2015-12-12PHP 動(dòng)態(tài)生成靜態(tài)HTML頁(yè)面示例代碼
這篇文章主要為大家分享下PHP 動(dòng)態(tài)生成靜態(tài)HTML頁(yè)面示例代碼,需要的朋友可以參考下2014-01-01