use wav with CombineMultipleMP3sTo

The place for "I can't figure out how to..." questions.
Post Reply
timimi
User
Posts: 4
Joined: Tue Apr 24, 2018 9:49 am
Are you a spambot?: no

use wav with CombineMultipleMP3sTo

Post by timimi » Tue Apr 24, 2018 9:55 am

Hello

I already use this function found in

https://github.com/JamesHeinrich/getID3 ... oinmp3.php

Code: Select all

function CombineMultipleMP3sTo($FilenameOut, $FilenamesIn) {
	foreach ($FilenamesIn as $nextinputfilename) {
		if (is_array($nextinputfilename)) {
			$nextinputfilename = $nextinputfilename[0];
		}
		if (!is_readable($nextinputfilename)) {
			echo 'Cannot read "'.$nextinputfilename.'"<BR>';
			return false;
		}
	}
	if ((file_exists($FilenameOut) && !is_writeable($FilenameOut)) || (!file_exists($FilenameOut) && !is_writeable(dirname($FilenameOut)))) {
		echo 'Cannot write "'.$FilenameOut.'"<BR>';
		return false;
	}
	require_once(dirname(__FILE__).'/../getid3/getid3.php');
	ob_start();
	if ($fp_output = fopen($FilenameOut, 'wb')) {
		ob_end_clean();
		// Initialize getID3 engine
		$getID3 = new getID3;
		foreach ($FilenamesIn as $nextinputfilename) {
			$startoffset = 0;
			$length_seconds      = 0;
			if (is_array($nextinputfilename)) {
				@list($nextinputfilename, $startoffset, $length_seconds)  = $nextinputfilename;
			}
			$CurrentFileInfo = $getID3->analyze($nextinputfilename);
			if ($CurrentFileInfo['fileformat'] == 'mp3') {
				ob_start();
				if ($fp_source = fopen($nextinputfilename, 'rb')) {
					ob_end_clean();
					$CurrentOutputPosition = ftell($fp_output);
					// copy audio data from first file
					$start_offset_bytes = $CurrentFileInfo['avdataoffset'];
					if ($startoffset > 0) { // start X seconds from start of audio
						$start_offset_bytes = $CurrentFileInfo['avdataoffset'] + round($CurrentFileInfo['bitrate'] / 8 * $startoffset);
					} elseif ($startoffset < 0) { // start X seconds from end of audio
						$start_offset_bytes = $CurrentFileInfo['avdataend'] + round($CurrentFileInfo['bitrate'] / 8 * $startoffset);
					}
					$start_offset_bytes = max($CurrentFileInfo['avdataoffset'], min($CurrentFileInfo['avdataend'], $start_offset_bytes));
					$end_offset_bytes = $CurrentFileInfo['avdataend'];
					if ($length_seconds > 0) { // seconds from start of audio
						$end_offset_bytes = $start_offset_bytes + round($CurrentFileInfo['bitrate'] / 8 * $length_seconds);
					} elseif ($length_seconds < 0) { // seconds from start of audio
						$end_offset_bytes = $CurrentFileInfo['avdataend'] + round($CurrentFileInfo['bitrate'] / 8 * $startoffset);
					}
					$end_offset_bytes = max($CurrentFileInfo['avdataoffset'], min($CurrentFileInfo['avdataend'], $end_offset_bytes));
					if ($end_offset_bytes <= $start_offset_bytes) {
						echo 'failed to copy '.$nextinputfilename.' from '.$startoffset.'-seconds start for '.$length_seconds.'-seconds length (not enough data)';
						fclose($fp_source);
						fclose($fp_output);
						return false;
					}
					fseek($fp_source, $start_offset_bytes, SEEK_SET);
					while (!feof($fp_source) && (ftell($fp_source) < $end_offset_bytes)) {
						fwrite($fp_output, fread($fp_source, min(32768, $end_offset_bytes - ftell($fp_source))));
					}
					fclose($fp_source);
				} else {
					$errormessage = ob_get_contents();
					ob_end_clean();
					echo 'failed to open '.$nextinputfilename.' for reading';
					fclose($fp_output);
					return false;
				}
			} else {
				echo $nextinputfilename.' is not MP3 format';
				fclose($fp_output);
				return false;
			}
		}
	} else {
		$errormessage = ob_get_contents();
		ob_end_clean();
		echo 'failed to open '.$FilenameOut.' for writing';
		return false;
	}
	fclose($fp_output);
	return true;
}
I would like to combined wav instead. How to make that.

Thanks

James Heinrich
getID3() v1 developer
Posts: 1462
Joined: Fri May 04, 2001 4:00 pm
Are you a spambot?: no
Location: Northern Ontario, Canada
Contact:

Re: use wav with CombineMultipleMP3sTo

Post by James Heinrich » Tue Apr 24, 2018 3:07 pm

It's actually harder to combine WAV files, since you would need to ensure bit-perfect alignment of the audio streams. You can be pretty crude with MP3 splicing since the bitstream is error-tolerant, if you're off by a few bytes then the player will simply discard the incorrect data. You can also legally combine files of different bitrates. However with WAV you would need to first confirm that both files are of the exact same format (channels, sample rate, bit depth, channel order, etc), and assuming they are you'd need to copy only the PCM audio stream portions (minus headers and footers), and then wrap a new header and footer around everything. While possible, it's simper than a simple append copy.

The simplest solution probably involves something like ffmpeg, you'll find many examples of a similar question, like this.

timimi
User
Posts: 4
Joined: Tue Apr 24, 2018 9:49 am
Are you a spambot?: no

Re: use wav with CombineMultipleMP3sTo

Post by timimi » Tue Apr 24, 2018 5:55 pm

Hello, Thanks for your reply Fast James Heinrich.

I explain the context. I use google translate: text to speech to generate the mp3, now when the text is too big I combine it with your function. But the mp3 sound is in 32 bit. I use php on windows.

Why I wanted to use the wav is because I can not increase the bitrate because I saw in your function "32768 hz". If I could increase the bitrate. I would be happy with the mp3.

James Heinrich
getID3() v1 developer
Posts: 1462
Joined: Fri May 04, 2001 4:00 pm
Are you a spambot?: no
Location: Northern Ontario, Canada
Contact:

Re: use wav with CombineMultipleMP3sTo

Post by James Heinrich » Tue Apr 24, 2018 6:31 pm

I think you're confusing a number of terms.
  • bit depth in terms of audio is the "precision" with which any analogue sound can be recorded. This is almost universally 16-bit (a standard established by CD-audio many decades ago). Very early digital audio used 8-bit (and sounded terrible), some high-end (e.g. studio recording, home theatre) applications can use 24-bit, sometimes even 32-bit), but 99% of the audio you'll encounter is 16-bit.
  • bit rate is how much data per second is used to store the audio. The perceived quality of the audio recording at any arbitrary bitrate will vary widely depending on the type of content, compression technology, so there's no magic number of what's "good".
  • sampling frequency, in your example 32kHz, defines the range of frequencies the audio file can reproduce. By definition, the highest audio frequency reproducible is half of the sampling frequency, so your example 32kHz file could contain audio up to 16kHz. CD audio is 44kHz, DVD audio is 48kHz; for speech 32kHz is more than plenty.

timimi
User
Posts: 4
Joined: Tue Apr 24, 2018 9:49 am
Are you a spambot?: no

Re: use wav with CombineMultipleMP3sTo

Post by timimi » Tue Apr 24, 2018 9:22 pm

Ok thanks for the clarification. I would like to speak about sampling frequency. I would like to say 32khz. How can I change audio to 44khz n the function?

Thanks again.

James Heinrich
getID3() v1 developer
Posts: 1462
Joined: Fri May 04, 2001 4:00 pm
Are you a spambot?: no
Location: Northern Ontario, Canada
Contact:

Re: use wav with CombineMultipleMP3sTo

Post by James Heinrich » Tue Apr 24, 2018 9:35 pm

Open it in your favorite audio edit, resample to the new frequency, and re-save. You will lose a (tiny) amount of quality with the resampling, and lose more quality re-saving from a compressed format (no quality loss if you re-save from uncompressed WAV to uncompressed WAV). Bottom line is you can't improve the quality beyond what you start with, at best you won't make it much worse.

But you implied you're working with synthesized voice audio from Google Translate. Voice is quite low-frequency, 32kHz (even 22kHz) is plenty fine for voice recordings.

timimi
User
Posts: 4
Joined: Tue Apr 24, 2018 9:49 am
Are you a spambot?: no

Re: use wav with CombineMultipleMP3sTo

Post by timimi » Wed Apr 25, 2018 1:14 am

Thank you for your prompt response. I thought I could automatically act on the file and change the frequency. But if it's impossible to do it with getid3, I'll do it with and remix the final file.
thanks again

Post Reply