CREATE YOUR AI CUM SLUT ON CANDY.AI TRY FOR FREE
x

Ren'Py How to get the length of a video from the game directory [Solved]

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
11,017
16,307
But I suppose Ren'Py should handle that anyway?
Not necessarily, because it's the kind of issue that can be really tricky due to pythoners weird mind.
The approach lead to simpler code, but also code way more complex to debug. You'll know about the error when it's triggered, not when it effectively happen. And this especially apply for this kind of error.

Imagine that you've something like this :
Python:
define someObject = SomeClass( "abcd" )

[a lot of code and labels]

label whatever:
    $ name = someObject.fullName()
The game will break at the last line, because it's where the str will be concatenated with the bytearray. But the error happen in the first line, when you pass a str as argument while you should have used a bytearray.

And the only way to find this is to manually trace back not the process, like the traceback is doing, but the code of the class to see where the value is changed and defined, then to search where the parameter have been set.
Now, imagine that you create the object dynamically, there by passing a variable instead of a literal value like in my example, and you'll have to investigate even deeper to find where the said variable is defined.

So, in this case, it can also be an incompatibility between two third party modules, and totally not related to Ren'Py. It's Ren'Py that use the modules, and Ren'Py that catch the error, hence all the references in the traceback, but everything in fact happen outside of it.



Import errors aside, moviepy seems like a heavy solution just to get the video duration. Think I would look at if I needed it for a project.
I agree on this. Since the whole issue is to know the duration, therefore to get an information available in the file headers, a module that limits to the interpretation of the said headers is enough.

Ideally I would even advice on many small modules ; each one caring about the headers for one format. This would lead to way simpler 100% Python code, and therefore have a bigger OS compatibility value. But once again it's not really an approach that match Python's spirit.
 
  • Like
Reactions: Tribe and gojira667

Tribe

Member
Game Developer
May 5, 2021
226
493
I finally got it to work! I've been playing around with it for a while and it seems to function as intended.
After reading gojira667's suggestion, I installed the MediaInfo and pymediainfo modules.

Using a simple function, I returned the duration of the clip.
Python:
init python:
    from pprint import pprint
    from pymediainfo import MediaInfo

    def Duration_Of_Clip(video_clip):

        local_media_info = MediaInfo.parse(os.path.join(config.gamedir, video_clip))
        local_track_duration = 0.00

        for track in local_media_info.tracks:
            if track.track_type == "Video":
                local_track_duration = (track.duration/1000) # converted from ms to seconds
            else: continue
        
        return local_track_duration
Then I use $ clip_duration = Duration_Of_Clip(video_clip)

With this information, we can safely set timers for clips before/as they are played for videos that have been added to the game by players, rather than relying on renpy.music.get_duration(), which requires the clip to be playing already.