Gstreamer query duration

hello all,

seems i’m missing something querying song duration through gstreamer.
i’ve seen this post http://www.ruby-forum.com/topic/206849 , and the
proof-of-concept app, as well as duration meters in other players (zik)
which use the same method. this code however:

pipeline = Gst::ElementFactory.make(“playbin2”)
pipeline.uri= “file://home/jk/ruby/test.mp3”
d = Gst::QueryDuration.new(Gst::Format::Type::TIME)
pipeline.query(d)
gstdur = d.parse[1]/1000000
p “gstdur= #{gstdur}”

returns:

“gstdur= -1”

at the moment i’m using the gem ‘mp3info’ for duration, and querying
the same file with mp3info, like this:

audio = “/home/jk/ruby/test.mp3”
Mp3Info.open(audio){|data| @infodur = data.length}
p “infodur= #{@infodur}”

returns:

“infodur= 227.5791875”

any ideas what i’m doing wrong in the gstreamer query?

-jk

2011/3/7 J. K. [email protected]:

pipeline.query(d)
the same file with mp3info, like this:

any ideas what i’m doing wrong in the gstreamer query?

I think you have fogotten to call play method on pipeline.

solutions. Best Open Source Mac Front-Ends 2023


ruby-gnome2-devel-en mailing list
[email protected]
ruby-gnome2-devel-en List Signup and Options


Vincent C.

I think you have fogotten to call play method on pipeline.

i had tried that, with the same result…

pipeline = Gst::ElementFactory.make(“playbin2”)
pipeline.uri= “file:/home/jk/ruby/test.mp3”
pipeline.play

d = Gst::QueryDuration.new(Gst::Format::Type::TIME)
pipeline.query(d)
gstdur = d.parse[1]/1000000

p “gstdur= #{gstdur}”

returns

“gstdur= -1”

strange

this also returns the same value of -1

pipeline = Gst::ElementFactory.make(“playbin2”)
pipeline.uri= “file:/home/jk/ruby/test.mp3”
bus = pipeline.bus
bus.add_watch {|bus, message|
case message.type
when Gst::Message::DURATION
dur = message
p dur.parse[1]/1000000
end
true}
pipeline.play

-j

2011/3/7 J. K. [email protected]:

audio = “/home/jk/ruby/test.mp3”
pipeline = Gst::ElementFactory.make(“playbin2”)
pipeline.uri= “file://home/jk/ruby/test.mp3”

I think you are missing “/” here. file:///home/jk/ruby/test.mp3
protocole(file)://path(/home/jk/ruby/test.mp3)

Posted via http://www.ruby-forum.com/.


Vincent C.

2011/3/7 J. K. [email protected]:

i thought the same, but oddly i get an error with the extra ‘/’, and
the file doesn’t play -

pipeline.uri= “file://home/jk/ruby/test.mp3”

[#<GLib::Error: Resource not found.>, “gstgiosrc.c(313):
gst_gio_src_get_stream ():
/GstPlayBin2:playbin20/GstURIDecodeBin:uridecodebin0/GstGioSrc:source:\nCould
not open location file:///jk/ruby/test.mp3 for reading: Error opening
file: No such file or directory”]
I guess you now miss “home”
file:///home/jk/ruby/test.mp3 and not file:///jk/ruby/test.mp3
Posted via http://www.ruby-forum.com/.


Vincent C.

this:

audio = “/home/jk/ruby/test.mp3”

pipeline = Gst::ElementFactory.make(“playbin2”)
pipeline.uri= GLib.filename_to_uri(audio)

plays just fine, but my duration problem is not solved… i still
return

“gstdur= -1”
-1

any ideas?

2011/3/7 J. K. [email protected]:

“gstdur= -1”
-1

any ideas?

Maybe you need some gstreamer plugins. Did you try other files, other
formats?

[email protected]
ruby-gnome2-devel-en List Signup and Options


Vincent C.

Vincent C. wrote in post #986024:

Maybe you need some gstreamer plugins. Did you try other files, other
formats?

tried a different .mp3, .flac, .ogg, and .wav - all play fine, but
give me the same
“-1” duration.

the plugins i have are the following:
gstreamer0.10-
ffmpeg
pitfdll
tools
fluendo-plugins-mp3-partner v.7.0.20100316
esd
nice
gnonlin
pulseaudio
alsa
plugins-base
packagekit
plugins-good
x
plugins-bad
plugins-base-apps
plugins-bad-multiverse
plugins-ugly
plugins-ugly-multiverse
plugins-good-dbg
plugins-base-dbg
plugins-bad-multiverse-dbg
plugins-ugly-multivers-dbg
libgstreamer-plugins-base0.10(and -dev)
gir1.0-gst-plugins-base-0.10
libgstreamer0.10-0(and -dev)

Vincent C. wrote in post #985992:

2011/3/7 J. K. [email protected]:

audio = “/home/jk/ruby/test.mp3”
pipeline = Gst::ElementFactory.make(“playbin2”)
pipeline.uri= “file://home/jk/ruby/test.mp3”

I think you are missing “/” here. file:///home/jk/ruby/test.mp3
protocole(file)://path(/home/jk/ruby/test.mp3)

i thought the same, but oddly i get an error with the extra ‘/’, and
the file doesn’t play -

pipeline.uri= “file://home/jk/ruby/test.mp3”

[#<GLib::Error: Resource not found.>, “gstgiosrc.c(313):
gst_gio_src_get_stream ():
/GstPlayBin2:playbin20/GstURIDecodeBin:uridecodebin0/GstGioSrc:source:\nCould
not open location file:///jk/ruby/test.mp3 for reading: Error opening
file: No such file or directory”]

when i remove the first ‘/’, the file is found and plays…

pipeline.uri= “file:/home/jk/ruby/test.mp3”

#> “gstdur= -1”
#> -1

2011/3/19 J. K. [email protected]:

before the query takes place.


ruby-gnome2-devel-en mailing list
[email protected]
ruby-gnome2-devel-en List Signup and Options

Instead of sleep I would have use GLib::Timeout.add method. It should
allows other events to be processed during the 0.125 s.

def getDuration
GLib::Timeout.add(125){
qd = Gst::QueryDuration.new(Gst::Format::Type::TIME)
@pipeline.query(qd)
@dur = qd.parse[1]/1000000000
if @dur<0
true#TODO : add a limit for the number of queries
else
false
end
}


Vincent C.

hi vincent -

nice solution - didn’t like the sleep much myself, i’ll give this a
shot.

thanks,

-j

hey all,

SOLVED

interesting, it turns out that there is a lag in the duration query
(at least with 1.8.7, gtk2 0.19.3 and gstreamer 0.10.x.) when i put the
query in the timeout loop for showing position, it would first show -1 a
couple of times, and then show the duration parsed to seconds after a
bit. i ended up solving my problem writing a simple getDuration method,
which includes an 1/8 of a second sleep, to let the pipeline get rolling
before the query takes place.

def getDuration
sleep 0.125
qd = Gst::QueryDuration.new(Gst::Format::Type::TIME)
@pipeline.query(qd)
@dur = qd.parse[1]/1000000000
end

not the prettiest thing in the world, but works like a charm.

-jk