This is the testing Godot forums! All forum posts unique to this forum will be deleted! Please use the main forums here for any posts you want to keep. All forum rules still apply.

Get length of Audio/Video (Media) in seconds?

mrwappamrwappa Posts: 2Member

I am currently running Godot 3.2, using AudioStreamPlayer and VideoPlayer to play Audio and Video.

In the docs for Godot 3.1 there is a section for AudioStream where it describes a get_length() method that returns length of an audio stream in seconds.
Is there any similiar solution for AudioStreamPlayer and VideoPlayer in Godot 3.2 so that I can get the duration of those two media types?

Tags :


  • MegalomaniakMegalomaniak Posts: 2,580Admin
    edited April 2020

    Deleted the duplicate topic, in the future don't be afraid to just contact us admins if you need help adding tags. But on that note, the little gear icon to the right of the title should allow you to edit the topic and add tags.

    No worry though, it's a small thing.

  • jbrooks79jbrooks79 Posts: 2,241Premoderated

    I am a total noob, and have never seen this part of documents before, but here is docus for AudioStreamPlayer, here is first link . .

    It says there's a sub-function, under AudioStreamPlayer called get_playback_position(), here is what it says . .

    float get_playback_position ( )

    Returns the position in the AudioStream in seconds.

    Maybe the code is AudioStreamPlayer.get_playback_position(), that code line MIGHT give you the number you are looking for, maybe there is a similar one, for VideoStreamPlayer . .

    It's a bit more complicated, VideoPlayer has this one . .

    float stream_position
    Setter set_stream_position(value)
    Getter get_stream_position()

    The current position of the stream, in seconds.

    I can't really access those things, I have problems making my character move as I want, it might require some tricky coding, not sure . . . :( <3 <3

  • jbrooks79jbrooks79 Posts: 2,241Premoderated

    Maybe the mods can help, is this the right way to ' get ' the stream_position number ?! <3 <3<3

  • MegalomaniakMegalomaniak Posts: 2,580Admin
    edited April 2020

    that would get the active position in the playback in seconds. I don't think there is a way to get the length from the player(I could be wrong though), but conceivably the resource file you are looking to play back would have that info in it.

  • TwistedTwiglegTwistedTwigleg Posts: 2,561Admin

    For audio stream players, I think, though have not tested, that you can get the length of whatever sample is playing using something like this:

    var audio_player = get_node("AudioStreamPlayer")
    # quick error checking:
    if (audio_player != null):
        if ( != null):
            var audio_length =
            print ("Audio is ", audio_length, " seconds long")

    For video players, it does not look like there is a way to get the length automatically. The VideoStream class does not provide a get_length function. That said, there is a stream_position function, which you can set and get. I'm not sure if it would work, since it assumes Godot clamps the stream_position variable to the length of the video player, but maybe you could use code like this to get the end position of a VideoPlayer node:

    var video_player = get_node("VideoPlayer")
    if (video_player != null):
        if (video_player .stream != null):
            var stored_length = video_player.stream_position
            video_player.stream_position = 99999999
            var video_length = video_player.stream_position
            print ("Video is ", video_length, " seconds long")
            # set the video back to whatever position it was prior to getting the length
            video_player.stream_position = stored_length
  • mrwappamrwappa Posts: 2Member

    Can confirm that using the audio stream's get_length will give you duration of the audio stream. Very nice!

    The solution for getting the video duration through VideoPlayer did not work. It would be a clever solution if what you said about Godot clamping the stream_position was true.

  • jbrooks79jbrooks79 Posts: 2,241Premoderated

    Oh, he wants the duration of the audio tracks, start to finish . .


Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.