Getting video parameters
Hi guys,
I would like to know how can I get video settings from a video file. I would like to take the bitrate and the duration of the video file (mpg) from itself.
Is this possible?, has anyone done it yet?
I have been googling and searching along the forums but I cannot find the answer to my question
Thanks in advance
[354 byte] By [
jorgecrra] at [2007-11-26 20:30:08]

# 1
Yes you can do it.
First create a player for a data source pointing to the file.
Once you have done that you can use the
getDuration()
from the player class to get the duration of the file
To get the Bit Rate call
getControl("BitRateControl")
on the same player object and then from the BitRateControl object that you have obtained call
[code]
getBitRate()
to obtain the bit rate
Does this help?
# 2
Thanks qUesT_foR_knOwLeDge,
Do I need to realize the player?, what I have done is the following:
String[] params = {"","",""};
MediaLocator mediaLocator = new MediaLocator("file:./"+fileName);
MediaPlayer mPlayer = new MediaPlayer();
mPlayer.setMediaLocator(mediaLocator);
params[0] = "VIDEO";
BitRateControl brc = (BitRateControl)mPlayer.getControl("BitRateControl");
int bitRate = brc.getBitRate();
params[1] = Integer.toString(bitRate);
Time duration = mPlayer.getDuration();
params[2] = duration.toString();
but a NullPointerException appears in "BitRateControl brc = (BitRateControl)mPlayer.getControl("BitRateControl");" indicating that the player has no such control.
I am a newbie in JMF and I don't where the failure can be.
Could you please help me with this?
Thnxxx
# 3
You can first call getTrackControls() on a Configured Processor.
On each track you can use the getControls( )
method to return an array of Control objects associated with the track
You can do this
BitRateControl bitratecontrol = null;
TrackControl[] trackcontrols = processor.getTrackControls();
Control[] controls = new Control[40];
for(int j = 0 ; j < trackcontrols.length ; j++)
{
controls = trackcontrols[j].getControls();//This method returns an arrayof Control //Objects associated with the given player instance
for(int i = 0 ; i < controls.length ; i++)
{
if(controls[i] instanceof BitRateControl)
{
bitratecontrol = ( BitRateControl ) controls[i];
System.out.println( bitratecontrol.getBitRate());
break;
}
}
}
# 4
Hi,
I have already checked your code and now the problem is
javax.media.NotConfiguredError: getTrackControls cannot be called before configured
I have this now:
DataSource dataSource = null;
Processor processor = null;
MediaLocator mediaLocator = new MediaLocator("file:./"+fileName);
try{
dataSource = Manager.createDataSource(mediaLocator);
processor = Manager.createProcessor(dataSource);
}catch(Exception e){
e.printStackTrace();
}
processor.configure();
BitRateControl bitratecontrol = null;
TrackControl[] trackcontrols = processor.getTrackControls();
Control[] controls = new Control[40];
for(int j = 0 ; j < trackcontrols.length ; j++)
{
controls = (Control[])trackcontrols[j].getControls();//This method returns anarrayof Control //Objects associated with the given player instance
for(int i = 0 ; i < controls.length ; i++)
{
if(controls instanceof BitRateControl)
{
bitratecontrol = ( BitRateControl ) controls;
System.out.println( bitratecontrol.getBitRate());
break;
}
}
}
I don't know if with configure() method of Processor, the Processor is already configured....
I will continue investigating... the thing is that it seems not to be very obvious the process to reach the controls (first MediaLocator... then Data Source... then Processor... then configure it...) so, for a person like not very familiar with JMF... for sure something is missing.
But you ar being very iluminating ;), only a little bit more....
Thanks!!!
# 5
Reply #3 the first line i wrote this"You can first call getTrackControls() on a Configured Processor."
# 6
The above code which you typed won't work.
Now the reason is this.
Once you call
processor.configure();
it goes to the next line. It doesn't block and the next lines
BitRateControl bitratecontrol = null;
TrackControl[] trackcontrols = processor.getTrackControls();
gets executed on a proecssor instance which has still not been configured though you have called processor.configure since processor.configure doesn't perform a blocking call.
In order to tackle this problem you have to implement a ControllerListener and override the controllerUpdate(ControllerEvent e) method and add the ControllerListener to the ControllerListener to the processor using
processor.addControllerListener(object_reference)
and you have to do this before you call configure.
In controllerUpdata() you have to wait for a ControllerEvent to be generated which you then have to check and see if it's a ConfigureCompleteEvent
DataSource dataSource = null;
Processor processor = null;
MediaLocator mediaLocator = new MediaLocator("file:./"+fileName);
try{
dataSource = Manager.createDataSource(mediaLocator);
processor = Manager.createProcessor(dataSource);
}catch(Exception e){
e.printStackTrace();
}
processor.addControllerListener(this);
processor.configure();
public void controllerUpdate(ControllerEvent e)
{
if(e instanceof ControllerEvent)
{
BitRateControl bitratecontrol = null;
TrackControl[] trackcontrols = processor.getTrackControls();
Control[] controls = new Control[40];
for(int j = 0 ; j < trackcontrols.length ; j++)
{
controls = (Control[])trackcontrols[j].getControls();//This method returns an array of Control //Objects associated with the given player instance
for(int i = 0 ; i < controls.length ; i++)
{
if(controls instanceof BitRateControl)
{
bitratecontrol = ( BitRateControl ) controls;
System.out.println( bitratecontrol.getBitRate());
break;
}
}
}
}
}
Incase you have found my help useful you can award me the dukes if you want to.
Message was edited by:
qUesT_foR_knOwLeDge
# 7
Hi, sorry for the delay...
I have execute the code as you wrote it... but now another error arises:
Exception in thread "JMF thread: SendEventQueue: com.sun.media.processor.unknown.Handler" javax.media.NotRealizedError: Cannot get CodecControl before reaching the realized state.
at com.sun.media.BasicTrackControl.getControls(BasicTrackControl.java:173)
at src.GeneralPanel.controllerUpdate(GeneralPanel.java:242)
at com.sun.media.BasicController.dispatchEvent(BasicController.java:1254)
at com.sun.media.SendEventQueue.processEvent(BasicController.java:1286)
at com.sun.media.util.ThreadedEventQueue.dispatchEvents(ThreadedEventQueue.java:65)
at com.sun.media.util.ThreadedEventQueue.run(ThreadedEventQueue.java:92)
As I said in previous posts, I am very new in JMF, and maybe there is some very obvious step that I am skipping... how can I realize the processor?.
Don't worry about your duke award, it is my first post in the java forums and I didn't know what was that for... I will rewards you :)
# 8
Post your entire code and post it within [url=" http://forum.java.sun.com/help.jspa?sec=formatting"]code tags[/url]
# 9
Finally I found a new solution for that problem, a new one with much fewer headaches!!!!
Instead of being lost in the middle of processors, mediaLocators, realized states, and a large etc.... I decided to take the output lines of a transcoder programme (ffmpeg), redirect them to a text file, take the line with the bitrate and time of the output file and use it in myt program.
I wish I could say that I have done it with JMF, but too much complicated.... maybe JMF for other tasks....
Thank you very much anyway... Your help has been very useful (now I know much more about JMF!!!)