php使用ffmpeg将.amr文件转换成.mp3文件

1.工具:FFmpeg

      FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序,功能很强大。

2.下载ffmpeg:http://ffmpeg.zeranoe.com/builds

3.配置环境变量

4.在cmd命令中输入 ffmpeg  –version  出现版本信息说明安装成功

5.测试是否可以转换 在cmd命令中输入 ffmpeg -i a.amr b.mp3

 出现3.图中的.mp3文件说名转换成功

6.用php执行外部命令   system()

 用ffmpeg查看视频的信息

//文件路径$file       KC_FFMPEG_PATH定义的常量   define('KC_FFMPEG_PATH', 'ffmpeg -i "%s" 2>&1');   需要环境变量配置到bin目录
function video_info($file) {
        ob_start();
        passthru(sprintf(KC_FFMPEG_PATH, $file));    
        $info = ob_get_contents();
        ob_end_clean();
      // 通过使用输出缓冲,获取到ffmpeg所有输出的内容。
       $ret = array();
        // Duration: 01:24:12.73, start: 0.000000, bitrate: 456 kb/s
        if (preg_match("/Duration: (.*?), start: (.*?), bitrate: (d*) kb/s/", $info, $match)) {
            $ret['duration'] = $match[1]; // 提取出播放时间
            $da = explode(':', $match[1]); 
            $ret['seconds'] = $da[0] * 3600 + $da[1] * 60 + $da[2]; // 转换为秒
            $ret['start'] = $match[2]; // 开始时间
            $ret['bitrate'] = $match[3]; // bitrate 码率 单位 kb
        }
    
        // Stream #0.1: Video: rv40, yuv420p, 512x384, 355 kb/s, 12.05 fps, 12 tbr, 1k tbn, 12 tbc
        if (preg_match("/Video: (.*?), (.*?), (.*?)[,s]/", $info, $match)) {
            $ret['vcodec'] = $match[1]; // 编码格式
            $ret['vformat'] = $match[2]; // 视频格式 
            $ret['resolution'] = $match[3]; // 分辨率
            $a = explode('x', $match[3]);
            $ret['width'] = $a[0];
            //$ret['height'] = $a[1];
        }
    
        // Stream #0.0: Audio: cook, 44100 Hz, stereo, s16, 96 kb/s
        if (preg_match("/Audio: (w*), (d*) Hz/", $info, $match)) {
            $ret['acodec'] = $match[1];       // 音频编码
            $ret['asamplerate'] = $match[2];  // 音频采样频率
        }
    
        if (isset($ret['seconds']) && isset($ret['start'])) {
            $ret['play_time'] = $ret['seconds'] + $ret['start']; // 实际播放时间
        }
    
        $ret['size'] = filesize($file); // 文件大小
        return $ret;
    }   

结果
 
原文地址:https://www.cnblogs.com/mengor/p/7596115.html