Getting the duration from video stream

2,399 views
Skip to first unread message

ryan

unread,
May 29, 2010, 8:10:04 AM5/29/10
to xuggler-users
Hi, I am trying to get the duration of a video from the container
using java. The problem is that I keep getting the default value of a
large negative number for the duration time (-923423...........)
instead of the time in millseconds. My code is below, can anyone help
me in getting the duration time? Thanks in advance

if (container.open(thefile.getStream(), null) < 0){
// Message: "could not open file"
throw new HeliosBusinessException(new
HeliosMessage("error.CouldNotOpenFile", this.getName()));
}

int numStreams = container.getNumStreams();

for(int i = 0; i < numStreams; i++){

IStream stream = container.getStream(i);
IStreamCoder coder = stream.getStreamCoder();
long dur = container.getDuration();
stream.getDuration();

//The stream and the container both bring back
this large negative number

// Get Video information
if (coder.getCodecType() == ICodec.Type.CODEC_TYPE_VIDEO){
String durationInUse = "Duration In Use: " + (stream.getDuration()
== Global.NO_PTS ? "unknown" : "" + stream.getDuration());

Dilip Shah

unread,
May 30, 2010, 12:50:20 PM5/30/10
to xuggler-users
Ryan,

The following function works for me:
/**
* Fetches a video's duration in microseconds.
*
* @param videoFile
* @return
*/
public long getVideoDuration(String videoFile) {
// Create a Xuggler container object
IContainer container = IContainer.make();

// Open up the container
if (container.open(videoFile, IContainer.Type.READ, null) < 0) {
throw new IllegalArgumentException("Could not open file: " + videoFile);
}

return container.getDuration();
}


Dilip

--------------------------------------------------
From: "ryan" <sean...@gmail.com>
Sent: Saturday, May 29, 2010 5:10 AM
To: "xuggler-users" <xuggle...@googlegroups.com>
Subject: [xuggler-users] Getting the duration from video stream

> --
> You received this message because you are subscribed to the Google Groups
> "xuggler-users" group.
> To post to this group, send email to xuggle...@googlegroups.com.
> To unsubscribe from this group, send email to
> xuggler-user...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/xuggler-users?hl=en.
>
>

Sean Ryan

unread,
May 30, 2010, 3:18:49 PM5/30/10
to xuggle...@googlegroups.com, xuggler-users
My code takes a DataInput stream instead of the String videofile. It
works with the String videofile but not for a DataInput stream:

(container.open(thefile.getStream(),null) < 0){


Sent from my iPhone

On 30 May 2010, at 05:50 p.m., "Dilip Shah" <dilip...@hotmail.com>
wrote:

Sean Ryan

unread,
May 31, 2010, 9:02:14 AM5/31/10
to xuggle...@googlegroups.com
Hi,

  So does anyone know exactly why when I try to use an InputStream to access the container that I can't get the duration time of a video?

// Uses (InputStream input, IContainerFormat format)
if (container.open(thefile.getStream(), null) < 0){
    throw new IllegalArgumentException("could not open file: " + filename);
}

// Value is large negative number instead of time in milliseconds
container.getDuration();

Thanks,

Ryan

Art Clarke

unread,
May 31, 2010, 12:24:34 PM5/31/10
to xuggle...@googlegroups.com
I do.  First search the lists for the reasons why (I believe I've answered this sometime last year) but duration calculation isn't as simple as it appears.

To try to get the most reliable estimate of duration, FFmpeg seeks to the end of file and back when a container doesn't report duration some other way.  In the case of an InputStream, you can't seek ahead, so we can't guess duration.

- Art
http://www.xuggle.com/
xu‧ggle (zŭ' gl) v. To freely encode, decode, and experience audio and video.

Use Xuggle to get the power of FFmpeg in Java.
Reply all
Reply to author
Forward
0 new messages