INTERACT FORUM

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: Expression Help [Playlist Time] (not total time)  (Read 1211 times)

stipus

  • Recent member
  • *
  • Posts: 7
Expression Help [Playlist Time] (not total time)
« on: April 11, 2021, 05:52:42 pm »

Hello,

I searched for a while and I couldn't manage to create a field that would show the time since the beginning of the playlist.

In short, I would like to add a field in my playlist view, that would show for each track the sum of all track durations since the first playlist track.

I know I can manually select all tracks from the first track to any track, and the sum of all durations appears in the summary at the bottom of the list. I would like to get that information for each track (shown in a field next to each track name).

Is that possible ?

Thanks,

stipus
Logged

zybex

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 2612
Re: Expression Help [Playlist Time] (not total time)
« Reply #1 on: April 12, 2021, 06:52:32 am »

Challenge accepted ;D

MC expressions are not designed to do these kind of things. For each file, an expression only sees the values corresponding to that particular file so it's not easy to get a cumulative runtime until a given point.

That said, global variables can be used to keep a running total, but it gets really complex really fast. The expression below seems to work, with caveats:

Code: [Select]
save(CustomData(#),_index)/
if(isequal([_index],0,4),,
if(isequal([_times],/[,7), save(,_times),)/
save(listbuild(1,;,listlimit([_times],Math([_index]-1),0), Math(int([duration,0])), listlimit([_times],10000,[_index])), _times)/
save(listmath(listlimit([_times],[_index],0),2),_sum)/
Math(int([_sum]/3600)):formatdate(Math([_sum]/86400),mm:ss))

The output is formatted as [H]:mm:ss (total number of hours, supports values above 24h)

caveats:
- your playlist needs to have the "sequence" column (file position in the list). I think it's always there for regular Playlists, but for a Smartlist you need to add the Sequence modifier to the playlist definition
- it's slow for huge playlists
- if you sort the playlist by a different order than the sequence number you might see some weird values. For short playlists it seems to work fine.
- fast-scrolling a large playlist may also cause wrong values, as MC will skip calculating the sum for some files in the middle
- not sure what happens when you have this expression on multiple playlists or on a Computed Field. It should work, try it out

Expression developed with ZELDA and tested as an Expression Column on a couple of playlists.

Good luck!
Logged

zybex

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 2612
Re: Expression Help [Playlist Time] (not total time)
« Reply #2 on: April 12, 2021, 07:10:01 am »

Here's a rundown of what the expression does, if anyone is interested.

The expression builds a List called [_times] which stores the duration of each file in the playlist (in seconds). As MC is displaying the playlist, it's also executing the expression for each file in order, updating the corresponding item in the List of durations and then returning the sum of all items until the current position.

Commented code:
// save sequence number of current file as [_index]
save(CustomData(#),_index)/
// if sequence number is invalid (blank or zero), return nothing
if(isequal([_index],0,4),,
// else: if the list of times is undefined, initialize [_times] to a blank list
if(isequal([_times],/[,7), save(,_times),)/
// change the value of the [_times] list item at the [_index] position
// this rebuilds the list with all values before the current position, plus the current duration, plus all values after the current position, then re-saves it as [_times]
// As MC executes this for each item in the playlist, the [_times] list grows until it holds a value for each item
// Note the "10000" value - if you use this on larger playlists, just increase it to a larger value

save(listbuild(1,;,listlimit([_times],Math([_index]-1),0), Math(int([duration,0])), listlimit([_times],10000,[_index])), _times)/
// calculate the sum of all items in the list until the current position, save as [_sum] (in seconds)
save(listmath(listlimit([_times],[_index],0),2),_sum)/
// formats the output, calculating the total number or hours and using FormatDate() for the minutes:seconds
Math(int([_sum]/3600)):formatdate(Math([_sum]/86400),mm:ss))


Note regarding Zelda: The CustomData(#) function always return blank in Zelda since there's no playlist context (no way to do that using MC API, as far as I know). So during testing one has to use some test/fake numbers as replacement.
Logged

Matt

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 42372
  • Shoes gone again!
Re: Expression Help [Playlist Time] (not total time)
« Reply #3 on: April 12, 2021, 07:38:40 am »

Look at the variables:
Playlist Remaining Time
Playlist Total Time

In the player window.  I think they already do this.
Logged
Matt Ashland, JRiver Media Center

zybex

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 2612
Re: Expression Help [Playlist Time] (not total time)
« Reply #4 on: April 12, 2021, 08:01:39 am »

That shows the total/remaining total, but not the cumulative time until a given item (that would be Played Time, and would only work while the list was playing)
Logged

Matt

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 42372
  • Shoes gone again!
Re: Expression Help [Playlist Time] (not total time)
« Reply #5 on: April 12, 2021, 08:22:40 am »

Because I love an expression challenge, a coming Media Center will allow using a new function PlaylistTime() that will output the time of the track in the playlist (a sum of all previous track durations).

History:
NEW: Added the PlaylistTime(...) expression to get the time of a track in the current playlist (a sum of all previous durations).

:)
Logged
Matt Ashland, JRiver Media Center

zybex

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 2612
Re: Expression Help [Playlist Time] (not total time)
« Reply #6 on: April 12, 2021, 08:30:16 am »

Nice! Next time I'll wait for you to do it first  8)
Logged

zybex

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 2612
Re: Expression Help [Playlist Time] (not total time)
« Reply #7 on: April 12, 2021, 08:36:29 am »

(edited)

May I suggest:
PlayListTime(mode)

mode:
0 - cumulative start time of this track (your current implementation; track 1 returns 0:0)
1 - cumulative runtime until this track (including it) = mode 0 + current track
2 - remaining time on the playlist after current track
3 - remaining time on the playlist including current track = mode 2 + current track
4 - total playlist time
Logged

Matt

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 42372
  • Shoes gone again!
Re: Expression Help [Playlist Time] (not total time)
« Reply #8 on: April 12, 2021, 08:53:04 am »

Take two!

Coming later:
NEW: Added the PlaylistTime(...) expression to get the time of a track in the current playlist (a sum of all previous durations) in mode 0, and the remaining time of the playlist in mode 1 and 2 (1 excludes the current track), and mode 3 for the total playlist duration.
Logged
Matt Ashland, JRiver Media Center

Matt

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 42372
  • Shoes gone again!
Re: Expression Help [Playlist Time] (not total time)
« Reply #9 on: April 12, 2021, 08:55:14 am »

Take three :)

NEW: Added the PlaylistTime(...) expression to get the time of a track in the current playlist (a sum of all previous durations) in mode 0, mode 1 adds the current track as well, the remaining time of the playlist in mode 1 and 2 (1 excludes the current track), and mode 3 for the total playlist duration.
Logged
Matt Ashland, JRiver Media Center

zybex

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 2612
Re: Expression Help [Playlist Time] (not total time)
« Reply #10 on: April 12, 2021, 08:56:49 am »

Is that 0 to 3 or 0 to 4?
Logged

Matt

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 42372
  • Shoes gone again!
Re: Expression Help [Playlist Time] (not total time)
« Reply #11 on: April 12, 2021, 09:02:55 am »

Updated the history!

NEW: Added the PlaylistTime(...) expression to get the time of a track in the current playlist (a sum of all previous durations) in mode 0, mode 1 adds the current track as well, the remaining time of the playlist in mode 2 and 3 (2 excludes the current track), and mode 4 for the total playlist duration.
Logged
Matt Ashland, JRiver Media Center

zybex

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 2612
Re: Expression Help [Playlist Time] (not total time)
« Reply #12 on: April 12, 2021, 09:12:14 am »

Nice :)

Matt, how is the output formatted for sums larger than 24h? The FormatTime FormatDate() function doesn't support that (maybe you could add an "hhh" format for total number of hours, ie, "hhh:mm:ss")
Come to think of that... how does MC display a [duration] above 24h for an hypothetical huge track? Audiobooks might reach that.
Logged

Matt

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 42372
  • Shoes gone again!
Re: Expression Help [Playlist Time] (not total time)
« Reply #13 on: April 12, 2021, 09:16:54 am »

Nice :)

Matt, how is the output formatted for sums larger than 24h? The FormatTime() function doesn't support that (maybe you could add an "hhh" format for total number of hours, ie, "hhh:mm:ss")
Come to think of that... how does MC display a [duration] above 24h for an hypothetical huge track? Audiobooks might reach that.

It's using our duration formatting code, so I think it will switch to "days" and "years" if there are a lot.
Logged
Matt Ashland, JRiver Media Center

stipus

  • Recent member
  • *
  • Posts: 7
Re: Expression Help [Playlist Time] (not total time)
« Reply #14 on: April 12, 2021, 11:24:45 am »

@zybex: Thanks a lot for accepting the challenge. Your expression works fine !!!
@matt: Less than 24 hours after I posted my problem... you planned a new release with the PlayListTime() function... Incredible !
 :o
Logged
Pages: [1]   Go Up