Create Sample Audio Clip

Post Reply
serihon
User
Posts: 10
Joined: Tue Nov 20, 2012 1:18 pm
Are you a spambot?: no

Create Sample Audio Clip

Post by serihon » Wed Nov 28, 2012 7:30 pm

Hey everyone!

I have written a bit of code using getID3 that creates a 30 second audio clip from a full length song, but currently only for .mp3's. Is it possible to modify this script at all to support more audio formats? I am not sure where to go from here to support other formats.

Thanks.

Code: Select all

function sampleFile($filename,$length){
	$getID3 = new getID3();
	
	$id3_info = $getID3->analyze($filename);
	
	list($t_min, $t_sec) = explode(':', $length);
	$time = ($t_min * 60) + $t_sec;
	
	$preview = $time / 30; // Preview time of 30 seconds
	
	$handle = fopen($filename, 'r');
	$content = fread($handle, filesize($filename));
	
	$length = strlen($content);
	
	$length = round(strlen($content) / $preview);
	$content = substr($content, $length / 3 /* Start extraction ~10 seconds in */, $length);
	
	$filename2 = str_replace('.','_sample.',$filename);
	//copy($filename,$filename2);
	$newFile = fopen($filename2,'w+');
	fwrite($newFile,$content);
	fclose($newFile);
}

serihon
User
Posts: 10
Joined: Tue Nov 20, 2012 1:18 pm
Are you a spambot?: no

Re: Create Sample Audio Clip

Post by serihon » Wed Nov 28, 2012 8:01 pm

I have confirmed that this code also works for .wav files. Still need to do .ogg at least if not other types.

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

Re: Create Sample Audio Clip

Post by James Heinrich » Wed Nov 28, 2012 9:27 pm

MP3 audio streams are error-tolerant so that will work fine (with probably a few hundred bytes of garbage at the end that will safely be ignored). Other file formats may (possibly) degrade less gracefully if they're truncated. Graceful degradation is likely also player-dependant.

I'm not sure why you're calling $getID3->analyze($filename) and then ignoring the results. You could at least use the analyzed value for playtime_seconds.

serihon
User
Posts: 10
Joined: Tue Nov 20, 2012 1:18 pm
Are you a spambot?: no

Re: Create Sample Audio Clip

Post by serihon » Thu Nov 29, 2012 12:15 pm

I was originally using the analyze($filename) to get the time of the clip but it wouldn't always pull it as different audio files I guess have different tags so I had to work around it.

Is there another alternative to getIDE3 that can create sample audio clips from full length files that you could point me towards if getIDE3 will not work for what I intend?

Thanks!

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

Re: Create Sample Audio Clip

Post by James Heinrich » Thu Nov 29, 2012 1:09 pm

serihon wrote:I was originally using the analyze($filename) to get the time of the clip but it wouldn't always pull it as different audio files I guess have different tags so I had to work around it.
getID3 should always return a correct value for playtime_seconds -- if you have a sample file where that doesn't work then please send it to me.
The playtime value is (almost) never derived from any tag format but from the file itself.

serihon
User
Posts: 10
Joined: Tue Nov 20, 2012 1:18 pm
Are you a spambot?: no

Re: Create Sample Audio Clip

Post by serihon » Thu Nov 29, 2012 1:24 pm

I will revert back to playtime_seconds and see if I can cause the error I was getting before.

Is it possible to overwrite the playtime_seconds on a music file to a shorter amount and thus make the song only play a sample of the original file? If so what file types are supported?

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

Re: Create Sample Audio Clip

Post by James Heinrich » Thu Nov 29, 2012 1:40 pm

getID3 will always return playtime_seconds as accurately represents the file, to the best of its ability.

If you mean somehow rewriting the header of an audio file in some DRM-like trickery to make a player only play a subset of the file -- no, that doesn't exist for any file format and even if it did you couldn't rely that all players would respect such a flag. Your approach of truncating the file at the approximate 30-second mark is a reasonable universal approach.

serihon
User
Posts: 10
Joined: Tue Nov 20, 2012 1:18 pm
Are you a spambot?: no

Re: Create Sample Audio Clip

Post by serihon » Thu Nov 29, 2012 1:46 pm

James Heinrich wrote: Your approach of truncating the file at the approximate 30-second mark is a reasonable universal approach.
What file types do you think my approach would work for using getIDE3? Just curious if you would know off the top of your head.

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

Re: Create Sample Audio Clip

Post by James Heinrich » Thu Nov 29, 2012 3:21 pm

Off the top of my head I'd say it should work for pretty much any audio/video file format. At worst a player might complain that the file appears to be truncated. Simplest way is to just try it and see what happens.

And it's getID3, not getIDE3.

serihon
User
Posts: 10
Joined: Tue Nov 20, 2012 1:18 pm
Are you a spambot?: no

Re: Create Sample Audio Clip

Post by serihon » Thu Nov 29, 2012 8:13 pm

It did work for Mp3 and wav (can't remember for sure about wav) but did not work for ogg and m4a. Those are the only ones I have attempted.

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

Re: Create Sample Audio Clip

Post by James Heinrich » Thu Nov 29, 2012 8:39 pm

MP3 is a format designed for streaming and highly tolerant of damage, so it's expected to work no matter how you slice and dice it apart or together. Most other formats are not this tolerant.

WAV = RIFF container. Once you get past the header is a sequence of amplitude values (assuming uncompressed PCM). Errors in the stream will manifest as clicks or other noise, as long as the number of bytes doesn't change during corruption (otherwise channel interleaving will cause havoc). Should behave reasonably well to truncation. If it's a compressed WAV file, truncation behaviour will likely be less good, but probably still OK (dependant on the codec used).

M4A = Quicktime container. Complex recursive file structure, I wouldn't be surprised if it misbehaves upon truncation. If you demux the audio data out of the M4A container into a raw AAC file, truncation should be on par with MP3, but you'll lose the metadata in the container (artist/title, etc)

Ogg (presumably Vorbis) file is divided into "pages" -- I don't have a lot of experience with truncated or otherwise corrupt Ogg files, I'm not sure how to predict behaviour.


In any case, behaviour is likely player-dependant -- it might fail to play with Winamp and Windows Media Player but play fine in foobar2000 or BSplayer (random examples).

Ideally the file should be properly rewritten to close off the file structure after the desired amount of audio.
Practically it would be much easier to generate "sample" files in a standard format, MP3 for example, irrespective of the input file format. This should be easy enough to automate (if needed) with command-line encoders and decoders appropriate to each format. I'm not sure if any CLI decoders will enable range clipping (to give you only 30 seconds) but if you encode to a CBR MP3 file it would be trivial to truncate the output MP3 to the necessary length:

Code: Select all

$mp3bitrate = 128000;
$sample_seconds = 30;
$sample_filesize = ($mp3bitrate / 8) * $sample_seconds;
file_put_contents('sample_filename.mp3', substr(file_get_contents($temp_encoded_mp3filename), 0, $sample_filesize));

serihon
User
Posts: 10
Joined: Tue Nov 20, 2012 1:18 pm
Are you a spambot?: no

Re: Create Sample Audio Clip

Post by serihon » Mon Dec 10, 2012 11:00 pm

Well thanks for the help James. I ended up installing ffmpeg on the server and used that to convert the audio files to mp3 and then shorten them with the code you supplied.

Post Reply