您只需要添加一个标题并附加 PCM 数据。
http://soundfile.sapp.org/doc/WaveFormat/
我找不到任何 PHP 库,所以我写了一个简单的 PHP 程序来做到这一点:
<?php
$pcm = file_get_contents('polly.raw');
//$pcm = $result->get('AudioStream')->getContents();
//Output file
$fp = fopen('file.wav', 'wb');
$pcm_size = strlen($pcm);
$size = 36 + $pcm_size;
$chunk_size = 16;
$audio_format = 1;
$channels = 1; //mono
/**From the AWS Polly documentation: Valid values for pcm are "8000" and "16000" The default value is "16000".
* https://docs.aws.amazon.com/polly/latest/dg/API_SynthesizeSpeech.html#polly-SynthesizeSpeech-request-OutputFormat
**/
$sample_rate = 16000; //Hz
$bits_per_sample = 16;
$block_align = $channels * $bits_per_sample / 8;
$byte_rate = $sample_rate * $channels * $bits_per_sample / 8;
/**
* http://soundfile.sapp.org/doc/WaveFormat/
* https://github.com/jwhu1024/pcm-to-wav/blob/master/inc/wave.h
* https://jun711.github.io/aws/convert-aws-polly-synthesized-speech-from-pcm-to-wav-format/
**/
//RIFF chunk descriptor
fwrite($fp, 'RIFF');
fwrite($fp,pack('I', $size));
fwrite($fp, 'WAVE');
//fmt sub-chunk
fwrite($fp, 'fmt ');
fwrite($fp,pack('I', $chunk_size));
fwrite($fp,pack('v', $audio_format));
fwrite($fp,pack('v', $channels));
fwrite($fp,pack('I', $sample_rate));
fwrite($fp,pack('I', $byte_rate));
fwrite($fp,pack('v', $block_align));
fwrite($fp,pack('v', $bits_per_sample));
//data sub-chunk
fwrite($fp, 'data');
fwrite($fp,pack('i', $pcm_size));
fwrite($fp, $pcm);
fclose($fp);
您也可以使用 FFmpeg 来实现这一点,但我的解决方案纯粹是用 PHP 编写的。
我希望我能帮助你!