How can I easily read video duration and bitrate in Java?

10,250

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!

Share:
10,250
trkich
Author by

trkich

Updated on June 04, 2022

Comments

  • trkich
    trkich almost 2 years

    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?

  • trkich
    trkich over 12 years
    Thanx for replay. I think before about this solution, i use ffmpeg and php for transcoding videos, but the problem with this with java is that user must install ffmpeg and that is a big problem because this is web applet and a lot of users will use this to upload videos and some of them maybe don't know how to install ffmpeg or they don't want to install it. I must find another solution...