3

I want to create a audio-video stream using Liquidsoap. And display the progess and total time of current track in the video. I wonder what is the best practice to achieve this. I am current using the following methods, in which:

  • Current progress is acquired with source.remaining function.
  • Total length is a global variable track_length, which is modified in on_track callback.

However, the current method has the following problems:

  • The return value of source.remaining does not change in a constant speed, as in the document metioned "estimation remaining time". In can be 19min, and suddenly jump to 19min20s, and then jump to 18min50. However, as the remaining time is less and less, the estimation becomes more accurate.
  • The track_length variable does get modified after the current track begins. However, the text drawing function which fetches the variable always get the initial value and never changed.

Thanks for your help!

Here is the relavant part of my script:

# Define the global variable to store the length of current track
track_length = 0

# Define the on_track callback which updates track_length
def process_metadata(metadata)
    file_name = metadata["filename"]
    track_length = file.duration(file_name)
end

# Define the audio source and hook on_track callback
audio = fallback([sequence([
    single("/etc/liquidsoap/lssj1.mp3")
])])
audio = on_track(process_metadata, audio)

# Define the function which returns the text to render
def get_time()
    "$(cur)/$(total)" % [("cur", string_of(source.remaining(audio))), ("total", string_of(track_length))]
end

# Create the video frame
video = fallback([blank()])
video = video.add_text.sdl(x=0, y=300, size=40, get_time, video)
Harry Summer
  • 215
  • 1
  • 7
  • Whoever with Liquidsoap >= 1.4.0 reads this: `file.duration` now is called `request.duration`. – david Oct 27 '20 at 12:38

1 Answers1

0

There is no such term 'global variable' in liquidsoap

No assignment, only definitions. x = expr doesn't modify x, it just defines a new x. The expression (x = s1 ; def y = x = s2 ; (x,s3) end ; (y,x)) evaluates to ((s2,s3),s1).

Link: https://www.liquidsoap.info/doc-dev/language.html

So, you should use references:

Define:

track_length = ref 0

then modify it (note we also use int_of_float):

track_length := int_of_float(file.duration(file_name))

then get its value:

!track_length

I believe it will fix your problems