Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concat MP3/media audio files on amazon S3 server

I want to concatenate the files uploaded on Amazon S3 server. How can I do this.

Concatenation on local machine i can do using following code.

var fs = require('fs'),
    files = fs.readdirSync('./files'),
    clips = [],
    stream,
    currentfile,
    dhh = fs.createWriteStream('./concatfile.mp3');

files.forEach(function (file) {
    clips.push(file.substring(0, 6));  
});


function main() {
    if (!clips.length) {
        dhh.end("Done");
        return;
    }
    currentfile = './files/' + clips.shift() + '.mp3';
    stream = fs.createReadStream(currentfile);
    
    stream.pipe(dhh, {end: false});
    stream.on("end", function() {
        main();        
    });
}


main();
like image 590
ghost... Avatar asked Jul 07 '15 12:07

ghost...


People also ask

Can S3 store audio files?

You can also store the files on Amazon Simple Storage Service (Amazon S3) or on Amazon CloudFront, which is a global content delivery network (CDN). For audio files stored on the WordPress server, files are broadcast directly from the server. For files stored in an S3 bucket, files are broadcast from the bucket.

Can Amazon S3 store media files?

Amazon's Simple Storage System (S3) provides a simple, cost-effective way to store static files. This tutorial shows how to configure Django to load and serve up static and user uploaded media files, public and private, via an Amazon S3 bucket.

How do I upload audio files to Amazon S3?

In the S3 dashboard, click the created the bucket name and open its bucket management interface. Drag the folder that contains the audio player files to the bucket management interface. In the Upload dialog, step 2 Set permissions, select the option Grant public read access to this object(s).


1 Answers

You can achieve what you want by breaking it into two steps:

Manipulating files on s3

Since s3 is a remote file storage, you can't run code on s3 server to do the operation locally (as @Andrey mentioned). what you will need to do in your code is to fetch each input file, process them locally and upload the results back to s3. checkout the code examples from amazon:

var s3 = new AWS.S3();
var params = {Bucket: 'myBucket', Key: 'mp3-input1.mp3'};
var file = require('fs').createWriteStream('/path/to/input.mp3');
s3.getObject(params).createReadStream().pipe(file);

at this stage you'll run your concatenation code, and upload the results back:

var fs = require('fs');
var zlib = require('zlib');

var body = fs.createReadStream('bigfile.mp3').pipe(zlib.createGzip());
var s3obj = new AWS.S3({params: {Bucket: 'myBucket', Key: 'myKey'}});
s3obj.upload({Body: body}).
   on('httpUploadProgress', function(evt) { console.log(evt); }).
   send(function(err, data) { console.log(err, data) });

Merging two (or more) mp3 files

Since MP3 file include a header that specifies some information like bitrate, simply concatenating them together might introduce playback issues. See: https://stackoverflow.com/a/5364985/1265980

what you want to use a tool to that. you can have one approach of saving your input mp3 files in tmp folder, and executing an external program like to change the bitrate, contcatenate files and fix the header. alternatively you can use an library that allows you to use ffmpeg within node.js.

in their code example shown, you can see how their merge two files together within the node api.

ffmpeg('/path/to/part1.avi')
  .input('/path/to/part2.avi')
  .input('/path/to/part2.avi')
  .on('error', function(err) {
    console.log('An error occurred: ' + err.message);
  })
  .on('end', function() {
    console.log('Merging finished !');
  })
  .mergeToFile('/path/to/merged.avi', '/path/to/tempDir'); 
like image 190
Ereli Avatar answered Oct 10 '22 23:10

Ereli