从 Grails 控制器渲染视频内容

发布于 2024-12-18 04:28:21 字数 471 浏览 2 评论 0原文

毫无疑问又是一个愚蠢的新手问题!我在 Grails 控制器中有一个字节数组,其中包含视频文件(确切地说是 *.mp4 文件)的内容。我熟悉如何从 grails 控制器渲染 JSON、XML 和其他基本类型,但我找不到任何显示如何输出视频的示例。本质上我想做以下事情:

  render bytes as MP4

我意识到我可能需要一个构造,例如:

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

但我不清楚如何获取其中的字节数组。显然我不是渲染类似 html 内容的专家。我隐藏在库函数后面太久了!任何指向参考或示例的指针将不胜感激。

因此,如果它有助于将建议指向正确的方向,则视频中的字节来自我正在使用 jets3t 库读取的 S3 对象。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静赏你的温柔 2024-12-25 04:28:21
    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()

那应该可以解决问题。

    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.

他不在意 2024-12-25 04:28:21

虽然当然可以从控制器提供视频,但如果您的目标只是在浏览器中呈现 QuickTime 视频,则可能有更简单的解决方案。在这种情况下,您可能希望尝试使用 FlashPlayer 插件,可通过以下命令使用:

grails install-plugin flash-player

后,您可以简单地将以下行插入到您的视图 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')}"/>

安装此播放器 使该插件与 Grails V2 一起使用只需花些功夫,但现在它已经就位,我意识到该插件帮助我避免了多少工作。如果您想了解更多信息,请访问 http://grails.org/FlashPlayer+Plugin

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文