Search code examples
grailsamazon-s3mime-typesvideo-encoding

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.


Solution

  •     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.