Discussion:
Bash Get Song Length
Storm Dragon
2011-09-07 19:07:57 UTC
Permalink
Hi,
Is there a way to get the length of a song into a bash variable?
thanks
Storm
--
Vinux Publicity Coordinator: http://www.vinuxproject.org/
Registered Linux user number 508465: http://counter.li.org/
My blog, Thoughts of a Dragon: http://www.stormdragon.us/
How many Internet mail list subscribers does it take to change a lightbulb? http://goo.gl/eO4PJ
"We sail the endless oceans, we sail the raging seas. The quest is never ending, it leads us to destiny."
Alestorm
Johannes Weißl
2011-09-07 19:22:07 UTC
Permalink
Hi,
Post by Storm Dragon
Is there a way to get the length of a song into a bash variable?
Of course, e.g. with mutagen-inspect from the python-mutagen package:
DURATION=$(mutagen-inspect "$file" | sed -n 's/^- .* \([0-9]*\(\.[0-9]\+\)\?\) seconds.*/\1/p')

If you only want to use cmus, you can add the song as only member of a
playlist and use:
DURATION=$(cmus-remote -C "save -e -p -" | sed -n 's/^duration \([0-9]*\)/\1/p')

does that solve your problem?


Johannes
Storm Dragon
2011-09-07 19:33:43 UTC
Permalink
Hi,
yes, that does the trick.
Thanks
Storm
--
Vinux Publicity Coordinator: http://www.vinuxproject.org/
Registered Linux user number 508465: http://counter.li.org/
My blog, Thoughts of a Dragon: http://www.stormdragon.us/
Follow me on Twitter: http://www.twitter.com/stormdragon2976
"Fell in a river of illusion
And apathy"
Godsmack
Post by Johannes Weißl
Hi,
Post by Storm Dragon
Is there a way to get the length of a song into a bash variable?
DURATION=$(mutagen-inspect "$file" | sed -n 's/^- .* \([0-9]*\(\.[0-9]\+\)\?\) seconds.*/\1/p')
If you only want to use cmus, you can add the song as only member of a
DURATION=$(cmus-remote -C "save -e -p -" | sed -n 's/^duration \([0-9]*\)/\1/p')
does that solve your problem?
Johannes
------------------------------------------------------------------------------
Using storage to extend the benefits of virtualization and iSCSI
Virtualization increases hardware utilization and delivers a new level of
agility. Learn what those decisions are and how to modernize your storage
and backup environments for virtualization.
http://www.accelacomm.com/jaw/sfnl/114/51434361/
Jason Woofenden
2011-09-07 19:47:10 UTC
Permalink
Post by Storm Dragon
Hi,
Is there a way to get the length of a song into a bash variable?
If you're wanting the length of the currently-playing song, you
might want to set up a status_display_program. It gets notified
when the play-state changes, including being told by cmus the
duration of the current track.

I'd bet there are cases when a duration is not passed, such as for
streaming radio stations.

- Jason
Storm Dragon
2011-09-07 20:40:24 UTC
Permalink
Hi,
If I did a status_display_program could I do something like:
length=$(echo "$info" | sed -n 's/^duration //p')
And if that worked would the length be in seconds or miliseconds or
something else?
thanks
Storm
--
Vinux Publicity Coordinator: http://www.vinuxproject.org/
Registered Linux user number 508465: http://counter.li.org/
My blog, Thoughts of a Dragon: http://www.stormdragon.us/
Vinux is Linux done right: http://www.vinuxproject.org/
"It's just the beast under your bed, in your closet, in your head"
Metallica
Post by Jason Woofenden
Post by Storm Dragon
Hi,
Is there a way to get the length of a song into a bash variable?
If you're wanting the length of the currently-playing song, you
might want to set up a status_display_program. It gets notified
when the play-state changes, including being told by cmus the
duration of the current track.
I'd bet there are cases when a duration is not passed, such as for
streaming radio stations.
- Jason
------------------------------------------------------------------------------
Using storage to extend the benefits of virtualization and iSCSI
Virtualization increases hardware utilization and delivers a new level of
agility. Learn what those decisions are and how to modernize your storage
and backup environments for virtualization.
http://www.accelacomm.com/jaw/sfnl/114/51434361/
Jason Woofenden
2011-09-07 21:56:35 UTC
Permalink
Post by Storm Dragon
Hi,
length=$(echo "$info" | sed -n 's/^duration //p')
No, status_display_program is a setting in cmus. You tell it what
program (shell script) you want run when the play state changes (eg
when a new track starts playing.)

When cmus knows the duration, it tells that program via a
command-line argument.

The command-line arguments alternate between keys and values, so
you've gotta look through for one that's "duration" and the
following argument will be the duration of the track.

Check out the example program cmus-status-display that comes with
cmus. It does this cool trick to turn those arguments into
variables:

while test $# -ge 2
do
eval _$1='$2'
shift
shift
done

Then you'll have $_duration with the length of the track in
seconds. (Unless cmus doesn't tell you the duration, as I would
expect from a streaming radio station or something.)

So if you want to be able to quickly grab the duration from other
scripts, you could write it to a file:

if -n "$_duration"
then
echo -n "$_duration" > "$HOME/tmp/cmus_cur_duration.txt"
fi

and you can read that file from your other scripts:

duration="$(cat "$HOME/tmp/cmus_cur_duration.txt")"


-- Jason
Storm Dragon
2011-09-07 22:19:13 UTC
Permalink
Hi,
well, the whole reason I need the length is for a small last.fm scrobble
script that uses lastfmsubmitd to do the actual scrobbling. It requires
the song length which is all that I am missing. Here's the script so
far, it's only a rough draft:
#!/bin/bash

file=$(echo "$info" | sed -n 's/^file //p')
artist=$(echo "$info" | sed -n 's/^tag artist //p')
album=$(echo "$info" | sed -n 's/^tag album //p')
title=$(echo "$info" | sed -n 's/^tag title //p')

if [ -n "$artist" -a -n "$album" ] ; then
if [ -n "$title" -a -n "$length" ] ; then
/usr/lib/lastfmsubmitd/lastfmsubmit --artist "$artist" --title "$title"
--album "$album" --length "$length"
fi
fi
exit 0
Thanks
Storm
--
Vinux Publicity Coordinator: http://www.vinuxproject.org/
Registered Linux user number 508465: http://counter.li.org/
My blog, Thoughts of a Dragon: http://www.stormdragon.us/
How many Internet mail list subscribers does it take to change a lightbulb? http://goo.gl/eO4PJ
"gimme gimme shock treatment! I wanna wanna shock treatment!"
Ramones
Post by Jason Woofenden
Post by Storm Dragon
Hi,
length=$(echo "$info" | sed -n 's/^duration //p')
No, status_display_program is a setting in cmus. You tell it what
program (shell script) you want run when the play state changes (eg
when a new track starts playing.)
When cmus knows the duration, it tells that program via a
command-line argument.
The command-line arguments alternate between keys and values, so
you've gotta look through for one that's "duration" and the
following argument will be the duration of the track.
Check out the example program cmus-status-display that comes with
cmus. It does this cool trick to turn those arguments into
while test $# -ge 2
do
eval _$1='$2'
shift
shift
done
Then you'll have $_duration with the length of the track in
seconds. (Unless cmus doesn't tell you the duration, as I would
expect from a streaming radio station or something.)
So if you want to be able to quickly grab the duration from other
if -n "$_duration"
then
echo -n "$_duration" > "$HOME/tmp/cmus_cur_duration.txt"
fi
duration="$(cat "$HOME/tmp/cmus_cur_duration.txt")"
-- Jason
------------------------------------------------------------------------------
Using storage to extend the benefits of virtualization and iSCSI
Virtualization increases hardware utilization and delivers a new level of
agility. Learn what those decisions are and how to modernize your storage
and backup environments for virtualization.
http://www.accelacomm.com/jaw/sfnl/114/51434361/
Johannes Weißl
2011-09-07 22:59:50 UTC
Permalink
Hi,
Post by Storm Dragon
well, the whole reason I need the length is for a small last.fm scrobble
script that uses lastfmsubmitd to do the actual scrobbling. It requires
the song length which is all that I am missing.
Here is my take:

------------------- cmus_lastfmsubmit -----------------------
#!/bin/bash

LASTFMSUBMIT=/usr/lib/lastfmsubmitd/lastfmsubmit

function map_args () {
case "$1" in
duration) echo "length" ;;
musicbrainz_trackid) echo "mbid" ;;
*) echo "$1" ;;
esac
}

args=()
while [ $# -ge 2 ] ; do
case "$1" in
artist|album|title|duration|musicbrainz_trackid)
args=("${args[@]}" "--$(map_args "$1")" "$2")
;;
esac
shift 2
done

$LASTFMSUBMIT "${args[@]}"
-------------------------------------------------------------

Just use it as "status_display_program" in cmus:
:set status_display_program=/path/to/cmus_lastfmsubmit

(or include in the status display script already present:
cmus_lastfmsubmit "$@" &
)

Does it work for you? I saved the draft to
http://cmus.sourceforge.net/wiki/doku.php?id=cmus_lastfmsubmit


Johannes
Storm Dragon
2011-09-08 01:23:17 UTC
Permalink
Hi,
This works great!
thanks
Storm
--
Vinux Publicity Coordinator: http://www.vinuxproject.org/
Registered Linux user number 508465: http://counter.li.org/
My blog, Thoughts of a Dragon: http://www.stormdragon.us/
Follow me on Twitter: http://www.twitter.com/stormdragon2976
"Listen up to ancient tales of monsters, zombies, ghosts 'bout creatures rising from the graves to haunt the living world"
The Other
Post by Johannes Weißl
Hi,
Post by Storm Dragon
well, the whole reason I need the length is for a small last.fm scrobble
script that uses lastfmsubmitd to do the actual scrobbling. It requires
the song length which is all that I am missing.
------------------- cmus_lastfmsubmit -----------------------
#!/bin/bash
LASTFMSUBMIT=/usr/lib/lastfmsubmitd/lastfmsubmit
function map_args () {
case "$1" in
duration) echo "length" ;;
musicbrainz_trackid) echo "mbid" ;;
*) echo "$1" ;;
esac
}
args=()
while [ $# -ge 2 ] ; do
case "$1" in
artist|album|title|duration|musicbrainz_trackid)
;;
esac
shift 2
done
-------------------------------------------------------------
:set status_display_program=/path/to/cmus_lastfmsubmit
)
Does it work for you? I saved the draft to
http://cmus.sourceforge.net/wiki/doku.php?id=cmus_lastfmsubmit
Johannes
------------------------------------------------------------------------------
Using storage to extend the benefits of virtualization and iSCSI
Virtualization increases hardware utilization and delivers a new level of
agility. Learn what those decisions are and how to modernize your storage
and backup environments for virtualization.
http://www.accelacomm.com/jaw/sfnl/114/51434361/
Loading...