您现在的位置是:群英 > 开发技术 > 编程语言
如何用PHP+ffmpeg实现对视频的处理操作
Admin发表于 2022-09-21 09:30:541050 次浏览
这篇文章给大家分享的是“如何用PHP+ffmpeg实现对视频的处理操作”,文中的讲解内容简单清晰,对大家学习和理解有一定的参考价值和帮助,有这方面学习需要的朋友,接下来就跟随小编一起学习一下“如何用PHP+ffmpeg实现对视频的处理操作”吧。

ffmpeg是一款开源、跨平台的视频处理程序,可用在Windows、mac、linux等平台,可以方便的运用多种语言脚本来调用其执行视频的操作。

下面介绍使用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";
 
//调用php的exec方法去执行脚本
exec($shell, $output, $return_val);
 
//获取输出信息
print_r($output);

FFmpeg获得视频文件的缩略图

function getVideoCover($file,$time,$name) {
   if(empty($time))$time = '1';//默认截取第一秒第一帧
   $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]; //播放时间
    $arr_duration = explode(':', $match[1]);
    $data['seconds'] = $arr_duration[0] * 3600 + $arr_duration[1] * 60 + $arr_duration[2]; //转换播放时间为秒数
    $data['start'] = $match[2]; //开始时间 
    $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']; //实际播放时间
  }
  $data['size'] = filesize($file); //文件大小
  return $data;
}
  
//用法
$video_info = getVideoInfo('video.mp4');
print_r($video_info);
?>

Fmpeg获得视频文件的总长度时间和创建时间

function getTime($file){
   $vtime = exec("ffmpeg -i ".$file." 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//");//总长度
   $ctime = date("Y-m-d H:i:s",filectime($file));//创建时间
   //$duration = explode(":",$time);
   // $duration_in_seconds = $duration[0]*3600 + $duration[1]*60+ round($duration[2]);//转化为秒
   return array('vtime'=>$vtime,
   'ctime'=>$ctime
   );
}

另外一种方法

ffprobe -v quiet -print_format json -show_format -show_streams test.mp4

结果

{
    "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"
        }
    }
}



以上就是关于如何用PHP+ffmpeg实现对视频的处理操作的介绍啦,需要的朋友可以参考上述内容,希望对大家有帮助,想要了解更多,欢迎关注群英网络,小编将为大家输出更多高质量的实用文章!

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

标签: ffmpeg
相关信息推荐
2022-06-07 17:33:26 
摘要:区别:对于普通函数,接收者为值类型时,不能将指针类型的数据直接传递,反之亦然;对于方法,接收者为值类型时,可以直接用指针类型的变量调用方法,反过来同样也可以。
2022-04-29 11:58:31 
摘要:给大家带来一篇关于python中shell如何逐行输入?的相关教程文章,内容涉及到Python、python教程等相关内容,更多关于python的内容希望能够帮助到大家。
2022-05-20 17:26:19 
摘要:Go语言作为服务器编程语言,很适合处理日志、数据打包、虚拟机处理、文件系统、分布式系统、数据库代理等;网络编程方面,Go语言广泛应用于Web应用、API应用、下载应用等;Go语言还可用于内存数据库和云平台领域的开发。
云活动
推荐内容
热门关键词
热门信息
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部