如何在Java中轻松读取视频时长和比特率?

发布于 2024-12-18 00:30:27 字数 185 浏览 0 评论 0原文

这是我的第一个 Java 项目。 我正在用 Java 创建批量 FTP 视频上传器小程序。

在开始上传视频之前,我需要从视频文件中读取视频时长和视频比特率。

我在 Google 上做了一些研究,但只找到了一些第三方库(jffmpeg、xugler 等)。

有没有更简单的方法可以使用 Java 获取这些信息?

This is my first project in Java.
I'm creating batch FTP video uploader applet in Java.

Before I start uploading videos I need to read the video duration and video bitrate from the video files.

I did some research on Google but I only found some third party libraries (jffmpeg, xugler etc..)

Is there any easier way to get this information using Java?

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

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

发布评论

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

评论(2

甲如呢乙后呢 2024-12-25 00:30:27

您需要使用第 3 方工具,例如 Xuggle 或直接购买 FFmpeg。 Java 中没有“本地”方法可以做到这一点。 (Java 在“媒体内容”方面的尝试,如 JMF,从未真正失败过。)编写自己的媒体文件解析器将是困难且耗时的——这是轻描淡写的说法。

不需要花费太多精力来处理 FFmpeg 并解析输出。我的应用程序做到了,尽管它只对持续时间感兴趣。这是一个简单的 CLI 示例:

manoa:~ stu$ ffmpeg -i test.flv 
ffmpeg version 0.8.6, Copyright (c) 2000-2011 the FFmpeg developers
  built on Nov 21 2011 15:43:35 with gcc 4.2.1 (Apple Inc. build 5664)
  configuration: --prefix=/opt/xtendx/local --enable-gpl --enable-pthreads --enable-version3 --enable-nonfree --disable-decoder=prores_lgpl --enable-libvpx --enable-libvorbis --enable-libfaac --enable-libmp3lame --enable-libx264 --enable-libxvid --disable-decoder=libvpx --enable-avfilter --enable-filters --arch=x86_64 --enable-runtime-cpudetect --extra-cflags=-I/opt/xtendx/local/include --extra-ldflags=-L/opt/xtendx/local/lib --enable-hwaccel=mpeg2_vaapi --enable-hwaccel=mpeg4_vaapi --enable-hwaccel=h263_vaapi --enable-hwaccel=wmv3_vaapi
  libavutil    51.  9. 1 / 51.  9. 1
  libavcodec   53.  7. 0 / 53.  7. 0
  libavformat  53.  4. 0 / 53.  4. 0
  libavdevice  53.  1. 1 / 53.  1. 1
  libavfilter   2. 23. 0 /  2. 23. 0
  libswscale    2.  0. 0 /  2.  0. 0
  libpostproc  51.  2. 0 / 51.  2. 0
[flv @ 0x101807c00] Estimating duration from bitrate, this may be inaccurate

Seems stream 0 codec frame rate differs from container frame rate: 1000.00 (1000/1) -> 23.98 (24000/1001)
Input #0, flv, from 'trailer_flash.flv':
  Metadata:
    duration        : 125
    width           : 640
    height          : 340
    videodatarate   : 500
    framerate       : 24
    videocodecid    : 4
    audiodatarate   : 128
    audiodelay      : 0
    audiocodecid    : 2
    canSeekToEnd    : true
  Duration: 00:02:04.88, start: 0.000000, bitrate: 640 kb/s
    Stream #0.0: Video: vp6f, yuv420p, 640x340, 512 kb/s, 23.98 tbr, 1k tbn, 1k tbc
    Stream #0.1: Audio: mp3, 44100 Hz, stereo, s16, 128 kb/s
At least one output file must be specified

因此,您只需要逐行解析此行并找到此行:

Duration: 00:02:04.88, start: 0.000000, bitrate: 640 kb/s

然后将其切碎即可开始!

You'll need to use a 3rd party tool, like Xuggle or shell out directly to FFmpeg. There is no 'native' way in Java to do this. (Java attempts at "media stuff", like JMF, have never really paned out.) Writing your own media file parser is going to be difficult and time consuming--and that is an understatement.

It doen't take much effort to shell out to FFmpeg and parse the output. My app does it, although it is only interested in the durration. Here is a simple CLI example:

manoa:~ stu$ ffmpeg -i test.flv 
ffmpeg version 0.8.6, Copyright (c) 2000-2011 the FFmpeg developers
  built on Nov 21 2011 15:43:35 with gcc 4.2.1 (Apple Inc. build 5664)
  configuration: --prefix=/opt/xtendx/local --enable-gpl --enable-pthreads --enable-version3 --enable-nonfree --disable-decoder=prores_lgpl --enable-libvpx --enable-libvorbis --enable-libfaac --enable-libmp3lame --enable-libx264 --enable-libxvid --disable-decoder=libvpx --enable-avfilter --enable-filters --arch=x86_64 --enable-runtime-cpudetect --extra-cflags=-I/opt/xtendx/local/include --extra-ldflags=-L/opt/xtendx/local/lib --enable-hwaccel=mpeg2_vaapi --enable-hwaccel=mpeg4_vaapi --enable-hwaccel=h263_vaapi --enable-hwaccel=wmv3_vaapi
  libavutil    51.  9. 1 / 51.  9. 1
  libavcodec   53.  7. 0 / 53.  7. 0
  libavformat  53.  4. 0 / 53.  4. 0
  libavdevice  53.  1. 1 / 53.  1. 1
  libavfilter   2. 23. 0 /  2. 23. 0
  libswscale    2.  0. 0 /  2.  0. 0
  libpostproc  51.  2. 0 / 51.  2. 0
[flv @ 0x101807c00] Estimating duration from bitrate, this may be inaccurate

Seems stream 0 codec frame rate differs from container frame rate: 1000.00 (1000/1) -> 23.98 (24000/1001)
Input #0, flv, from 'trailer_flash.flv':
  Metadata:
    duration        : 125
    width           : 640
    height          : 340
    videodatarate   : 500
    framerate       : 24
    videocodecid    : 4
    audiodatarate   : 128
    audiodelay      : 0
    audiocodecid    : 2
    canSeekToEnd    : true
  Duration: 00:02:04.88, start: 0.000000, bitrate: 640 kb/s
    Stream #0.0: Video: vp6f, yuv420p, 640x340, 512 kb/s, 23.98 tbr, 1k tbn, 1k tbc
    Stream #0.1: Audio: mp3, 44100 Hz, stereo, s16, 128 kb/s
At least one output file must be specified

So, you'll just need to parse this line-by-line and find this line:

Duration: 00:02:04.88, start: 0.000000, bitrate: 640 kb/s

Then chop that up and you are good to go!

陌路终见情 2024-12-25 00:30:27

您可以使用 ffmpeg 项目中的 ffprobe 来获取多媒体文件的信息并获得良好的 JSON 格式的输出。

请查看此答案作为示例。

You can use ffprobe from the ffmpeg project, to get the information of your multimedia files and get the a nicely JSON formated output.

Take a look at this answer for an example.

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