开发者

mp3 snippets on s3

I need a solution to play a segment of an mp3. I have a few 1,000 audio files which are currently stored on Amazon S3, and would like to allow users to play them, however I would like to limit the play length to 30 seconds or so in the middle of t开发者_开发知识库he recording.

I'm not sure if I need to create an entirely new file (snippet) such as I would for a thumbnail if it were an image, or if it's possible using some player/steam to safely limit it that way so they cannot access the whole song.

I'm coming from a Rails environment and using Paperclip to handle the files and JPlayer to play them if it matters.

Any pointers or best practices?


This is possible by using the HTTP "Content-range" header. This header says 'please just give me the bytes from here to here and ignore the rest'. If the web server is set up to handle them (Apache is for instance), then you get a 206 response with a body of just those bytes.

You must create a small proxy application that effectively acts as a gateway between the listener and Amazon.

To see if your host will respond try this from the command line:

curl -v -I http://www.mfiles.co.uk/mp3-downloads/01-Tartaros%20of%20light.mp3

Where the url is one of yours. If you are lucky you will see:

Accept-Ranges: bytes
Content-Length: 5284483

This means that the server does accept the Content-range header and the full length of the file is 5284483 bytes long.

Let's request the first third of the file:

curl -H'Range: bytes=0-1761494' http://www.mfiles.co.uk/mp3-downloads/01-Tartaros%20of%20light.mp3 > /tmp/test1.mp3

You should now be able to play /tmp/test1.mp3 and hear the first third of the track.

The next step is to create a proxy application. A good approach would be to use https://github.com/aniero/rack-streaming-proxy but you would probably need to fork the project to send the 'Range: bytes=0-1761494' header. Alternatively have a look at Sinatra.

A bonus here is that because you are proxying the remote server, you could obfuscate the actual URL of the file by having a simple database table with an ID for each file. I would suggest writing a small script that also stores the byte length of each file, so that you don't have to calculate the range for each request.

Thus a GET to "/preview/12345" would proxy "http://amazon.com/my_long_url" and give you just the first third of the file.

On top of that, you could put Varnish in front of your own server, which would cache these partial MP3 files and mean that you are not having to constantly go back to Amazon to get the files.


Unfortunately, you'll need to make new snippets - there isn't really a way to tell a user's browser "download this entire mp3 file, but only play and allow access to the middle 30 seconds".


i think it is simplier to solve the problem in the client side.

Are you using flash to play the audio files?

If yes, I have done something similar (but with videos) using JWPlayer (it also supports audio files).

You can develop a custom plugin to control the snippet you want to play and then stop the audio file and show a message or something like that.

This solution combined with signed urls or/and rtmp streaming with CloudFront can be very safe.


Due to the mp3 format limitation, you cannot seek to the arbitrary frame in the middle of the song and start transmission from that point.

So, there are basically three options:

    1. Create new files offline. Very easy, but space consuming.
    2. Transcode files on the fly. CPU consuming, degrades quality.
    3. Limit playback with first X seconds: just peek into the song' header, get its bitrate and calculate size of the byte chunk to serve

And don't ever transmit more than you need: people will manage to intercept the stream and save it to disk (business side); save your users' traffic (good karma).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜