# Gstreamer query duration

**URL:** <https://www.ruby-forum.com/t/gstreamer-query-duration/204193>\
**Category:** Ruby-Gnome 2\
**Created:** [March 7, 2011, 4:45pm UTC](https://www.ruby-forum.com/t/gstreamer-query-duration/204193 "2011-03-07T16:45:19Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [March 7, 2011, 4:45pm UTC](https://www.ruby-forum.com/t/gstreamer-query-duration/204193/1 "2011-03-07T16:45:19Z")

</div>

hello all,

seems i’m missing something querying song duration through gstreamer.  
i’ve seen this post [http://www.ruby-forum.com/topic/206849](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

---

<div class="post-metadata">

**Author:** ![j\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@j\_k](https://www.ruby-forum.com/u/j_k)\
**Post date:** [March 7, 2011, 5:16pm UTC](https://www.ruby-forum.com/t/gstreamer-query-duration/204193/2 "2011-03-07T17:16:49Z")

</div>

2011/3/7 J. K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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. [http://p.sf.net/sfu/progress-d2d](http://p.sf.net/sfu/progress-d2d)
> 
> * * *
> 
> ruby-gnome2-devel-en mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [https://lists.sourceforge.net/lists/listinfo/ruby-gnome2-devel-en](https://lists.sourceforge.net/lists/listinfo/ruby-gnome2-devel-en)

–  
Vincent C.

---

<div class="post-metadata">

**Author:** ![j\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@j\_k](https://www.ruby-forum.com/u/j_k)\
**Post date:** [March 7, 2011, 5:31pm UTC](https://www.ruby-forum.com/t/gstreamer-query-duration/204193/3 "2011-03-07T17:31:34Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![j\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@j\_k](https://www.ruby-forum.com/u/j_k)\
**Post date:** [March 7, 2011, 5:50pm UTC](https://www.ruby-forum.com/t/gstreamer-query-duration/204193/4 "2011-03-07T17:50:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![j\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@j\_k](https://www.ruby-forum.com/u/j_k)\
**Post date:** [March 7, 2011, 6:08pm UTC](https://www.ruby-forum.com/t/gstreamer-query-duration/204193/5 "2011-03-07T18:08:17Z")

</div>

2011/3/7 J. K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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/](http://www.ruby-forum.com/).

–  
Vincent C.

---

<div class="post-metadata">

**Author:** ![j\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@j\_k](https://www.ruby-forum.com/u/j_k)\
**Post date:** [March 7, 2011, 6:20pm UTC](https://www.ruby-forum.com/t/gstreamer-query-duration/204193/6 "2011-03-07T18:20:14Z")

</div>

2011/3/7 J. K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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/](http://www.ruby-forum.com/).

–  
Vincent C.

---

<div class="post-metadata">

**Author:** ![j\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@j\_k](https://www.ruby-forum.com/u/j_k)\
**Post date:** [March 7, 2011, 6:27pm UTC](https://www.ruby-forum.com/t/gstreamer-query-duration/204193/7 "2011-03-07T18:27:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![j\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@j\_k](https://www.ruby-forum.com/u/j_k)\
**Post date:** [March 7, 2011, 8:06pm UTC](https://www.ruby-forum.com/t/gstreamer-query-duration/204193/8 "2011-03-07T20:06:26Z")

</div>

2011/3/7 J. K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> “gstdur= -1”  
> -1
> 
> any ideas?

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

> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [https://lists.sourceforge.net/lists/listinfo/ruby-gnome2-devel-en](https://lists.sourceforge.net/lists/listinfo/ruby-gnome2-devel-en)

–  
Vincent C.

---

<div class="post-metadata">

**Author:** ![j\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@j\_k](https://www.ruby-forum.com/u/j_k)\
**Post date:** [March 7, 2011, 10:00pm UTC](https://www.ruby-forum.com/t/gstreamer-query-duration/204193/9 "2011-03-07T22:00:24Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![j\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@j\_k](https://www.ruby-forum.com/u/j_k)\
**Post date:** [March 7, 2011, 6:14pm UTC](https://www.ruby-forum.com/t/gstreamer-query-duration/204193/10 "2011-03-07T18:14:08Z")

</div>

Vincent C. wrote in post #985992:

> 2011/3/7 J. K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > 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

---

<div class="post-metadata">

**Author:** ![j\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@j\_k](https://www.ruby-forum.com/u/j_k)\
**Post date:** [March 19, 2011, 1:43pm UTC](https://www.ruby-forum.com/t/gstreamer-query-duration/204193/11 "2011-03-19T13:43:33Z")

</div>

2011/3/19 J. K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> before the query takes place.
> 
> * * *
> 
> ruby-gnome2-devel-en mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [https://lists.sourceforge.net/lists/listinfo/ruby-gnome2-devel-en](https://lists.sourceforge.net/lists/listinfo/ruby-gnome2-devel-en)

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.

---

<div class="post-metadata">

**Author:** ![j\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@j\_k](https://www.ruby-forum.com/u/j_k)\
**Post date:** [March 19, 2011, 5:09pm UTC](https://www.ruby-forum.com/t/gstreamer-query-duration/204193/12 "2011-03-19T17:09:08Z")

</div>

hi vincent -

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

thanks,

-j

---

<div class="post-metadata">

**Author:** ![j\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@j\_k](https://www.ruby-forum.com/u/j_k)\
**Post date:** [March 19, 2011, 3:54am UTC](https://www.ruby-forum.com/t/gstreamer-query-duration/204193/13 "2011-03-19T03:54:55Z")

</div>

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
