How to implement audio format conversion in php

藏色散人
Release: 2023-03-04 19:36:02
Original
3406 people have browsed it

php音频格式转换的实现方法:首先把ffmpeg放在phpStudy的WWW目录下;然后把ffmpeg下的bin目录添加到windows的环境变量中;最后通过ffmpeg转换音频格式即可。

How to implement audio format conversion in php

推荐:《PHP视频教程

windows+php+ffmpeg转换音频格式

近几天在做一个语音识别搜索功能的时候遇到了一个问题 ,发个文章记录一下,萌新第一次发博客,有不对的地方请各位大佬们多多指正!
前端用的微信的sdk生成的录音,然后用阿里的一句话识别转成文字,这里遇到的坑就是阿里不支持微信返回的音频格式,所以这里需要处理一下格式,经过了一顿百度,决定使用ffmpeg进行音频的转换
首先要下载ffmpg工具 [下载地址](http://ffmpeg.org/)
我为了方便调试我把ffmpeg放在phpStudy的WWW目录下了 How to implement audio format conversion in php:

How to implement audio format conversion in php

其中ffmpeg.php是我自己写的,代码贴在下面
之后把ffmpeg下的bin 目录添加到windows的环境变量,具体操作这里就不说了,学过java的都清楚,如记不清了可百度一下
话不多说,直接上代码.
Copy after login
<?php /**
 * @api音频文件格式转换-将silk转为wav
 * @author wyp
 */
header("content-type:text/html;charset=utf-8");
date_default_timezone_set(&#39;Asia/Shanghai&#39;);
define(&#39;BASE_PATH&#39;, str_replace(&#39;\\&#39;, &#39;/&#39;, realpath(dirname(__FILE__) . &#39;/&#39;)) . "/");
//域名
$domain = $_SERVER[&#39;SERVER_NAME&#39;];

/**
 * 由于每次访问时会将音频文件上传到服务器上,所以我这里先把之前上传到服务器的音频删除
 */
$dir = scandir(&#39;./&#39;);
foreach ($dir as $key => $value) {
    if (substr($value, -4) == '.wav') {
        if ($value == $_GET['filepath']) {
            continue;
        }
        $filemtime = fileatime($value);
        if (time() - $filemtime > 10) {
            unlink($value);
        }

    }
}

//接收文件
if (!isset($_GET['filepath'])) {
    echo json_encode([
        'code' => 400,
        'info' => '文件路径为空',
    ]);
    exit;
}
$filepath = $_GET['filepath'];
$in_time = date('Y-m-d-h-i-s', time());
@$copy = copy($filepath, './in_' . $in_time . ".silk");//这里将文件下载到本地服务器
if (!$copy) {
    echo json_encode([
        'code' => 400,
        'info' => '请检查文件路径',
    ]);
    exit;
}
$filepath = "in_" . $in_time . ".silk";
//输出文件名
$rank = "rec";
for ($i = 1; $i  200,
    'cmd' => $cmd,//执行的cmd
    'outfile' => "$domain/ffmpeg/bin/$outfile"//文件路径
]);
?>
Copy after login

                                   

The above is the detailed content of How to implement audio format conversion in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!