Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render video content from a Grails controller

No doubt another silly newb question! I have a byte array in a Grails controller that contains the contents of a video file (an *.mp4 file to be exact). I am familiar with how to render JSON, XML and other basic types from a grails controller but I can't find any examples showing how to output video. In essence I want to do the following:

  render bytes as MP4

I realize that I probably need a construct such as:

  render(text:"<xml>some xml</xml>",contentType:"video/mpeg",encoding:"UTF-8")

but I'm not clear how I get the byte array in there.Obviously I am not an expert on rendering html-like content. I've been hiding behind library functions too long! Any pointers to a reference or example would be much appreciated.

So if it helps point the advice in the right direction, the bytes with the video are coming from an S3 object that I am reading with the jets3t library.

like image 853
Rich Sadowsky Avatar asked Nov 05 '22 10:11

Rich Sadowsky


2 Answers

    OutputStream out = response.getOutputStream()
    //set up byte array
    byte[] content = yourS3Object.getBytes()


    response.setContentLength(content.size())
    response.addHeader("Content-disposition", "attachment; filename=${yourS3Object.fileName}")
    response.addHeader("Content-type", "video/quicktime")
    out.write(content)
    out.close()

That should do the trick.

like image 144
Oliver Tynes Avatar answered Nov 15 '22 11:11

Oliver Tynes


While it's certainly possible to serve video from a controller, there may be a much simpler solution if your goal is only to present QuickTime video from within the browser. In this case you might instead wish to try the FlashPlayer plug-in, available with the command:

grails install-plugin flash-player

Once you get this player installed, you can then simply insert the following lines into your view GSP:

<div id="test">
  <p>You need Flash Player installed  to play this media.</p>
</div>
<g:flashPlayer id="test" varFile="${createLinkTo(dir: 'movies', file: 'my.mov')}"/>

It took a little fiddling to get the plug-in to work with Grails V2, but now that it's in place I realize how much work that plug-in helped me to avoid. If you want to learn more, visit http://grails.org/FlashPlayer+Plugin

like image 41
user1137128 Avatar answered Nov 15 '22 11:11

user1137128