1

Is there a an elegant way to check a video's length in milliseconds when uploaded as a MultipartFile?

public long checkVideoLength(MultipartFile video) {
     long length = 0;
     if(video.getContentType().equals("video/mp4")) {
          //Resolve the video's length if it's an mp4
     }
     return length; 
}

I've been unable to find an elegant solution to this.

Jake Miller
  • 2,432
  • 2
  • 24
  • 39
  • 1
    I think [this](http://stackoverflow.com/a/3048229/1678362) may answer your question, although it's about audio streams. – Aaron Nov 24 '16 at 15:38

1 Answers1

0

You need a library that can read the file and tell you the length. For example, using Caprica's VLCJ, which wraps VLC, once you point a MediaPlayer to your file, you can ask it for its length in seconds. I am not sure whether this will work without an interface, though.

Another option is Xuggler, which wraps FFMPEG. There, you can write the following to open a file and ask for its duration:

IMediaReader reader = ToolFactory.makeReader(filename);
IContainer container = reader.getContainer();
if (container.queryStreamMetaData() >= 0) {
   long duration = container.getDuration(); // <-- got it
}

Note that there is a lot of variability in what you can expect from an .mp4 file (mp4 is a container, not an encoding) - so beware malformed or unsupported variants.

Both VLCJ and Xuggler are GPLv3; you may need to isolate your program from them as plugins unless your code will also be GPL'd, depending on how you interpret java linking.

Another option is to call a system script that provides you the number. This dispenses with Java integration, but forces you to keep the script updated and have its system dependency available. For ubuntu linux, these can work:

ffprobe file.mp4 -show_format_entry duration

or

avprobe file.mp4 -show_format_entry duration

or

$ mediainfo --Inform="Video;%Duration%"  [inputfile]

all of them found in this answer

Community
  • 1
  • 1
tucuxi
  • 17,561
  • 2
  • 43
  • 74
  • I tried to find the jars for Xuggler, but am unable to find them anywhere. The maven dependency also doesn't work as Xuggler was abandoned. Do you know of any alternatives or of a place I can acquire the Xuggler jars? – Jake Miller Nov 24 '16 at 16:00
  • If you can require ffmpeg to be pre-installed, using ffprobe may be the easy answer. Otherwise, I would go for VLC - it reads *everything* – tucuxi Nov 24 '16 at 16:09