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 :

Comments

  • MegalomaniakMegalomaniak Posts: 2,884Admin
    edited April 8

    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 . .

    https://docs.godotengine.org/en/stable/classes/class_audiostreamplayer.html

    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.

    https://docs.godotengine.org/en/stable/classes/class_audiostreamplayer.html#class-audiostreamplayer-method-get-playback-position

    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.

    https://docs.godotengine.org/en/stable/classes/class_videoplayer.html

    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,884Admin
    edited April 9

    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: 3,076Admin

    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 (audio_player.stream != null):
            var audio_length = audio_player.stream.get_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 . .

    <3

  • viminovimino Posts: 7Member
    edited September 17

    Tried this recently on Godot 3.2.3 and was sad to see that although it plays, stops and pauses fine, a VideoPlayer can get the position (I attached an example that shows the slider moving correctly) but can't seek, meaning that VideoPlayer.set_stream_position = ?does nothing. Note that I hard-coded the length of the stream (1m) as none of the suggestions worked but get_length will. It's in the source code. Didn't test with godot-videodecoder.

    As the seek is a known issue, it's a matter of time for both as it depends on this.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file