1

I'm merging multiple audio files (.wav) via this function but now I want to specify the bitrate of output file. What should I do?

function joinwavs($wavs) {
    $fields = join('/', array('H8ChunkID', 'VChunkSize', 'H8Format',
        'H8Subchunk1ID', 'VSubchunk1Size',
        'vAudioFormat', 'vNumChannels', 'VSampleRate',
        'VByteRate', 'vBlockAlign', 'vBitsPerSample'));
    $data = '';
    foreach ($wavs as $wav) {
        $wav = str_replace('http://example.com/', '', $wav);
        $fp = fopen($wav, 'rb');
        $header = fread($fp, 36);
        $info = unpack($fields, $header);
        // read optional extra stuff
        if ($info['Subchunk1Size'] > 16) {
            $header .= fread($fp, ($info['Subchunk1Size'] - 16));
        }
        // read SubChunk2ID
        $header .= fread($fp, 4);
        // read Subchunk2Size
        $size = unpack('vsize', fread($fp, 4));
        $size = $size['size'];
        // read data
        $data .= fread($fp, $size);
    }
    return ($header . pack('V', strlen($data)) . $data);
}
Krishna
  • 165
  • 1
  • 12

1 Answers1

0

Use FFMPEG for this. Don't edit the audio files directly, especially when you're doing more than just concatenating files.

You can either execute FFMPEG directly, or use the PHP extension. Personally, I've found the exec() route much easier.

Brad
  • 159,648
  • 54
  • 349
  • 530