From 072e802863ec3aa0c25870074ade078d988ae153 Mon Sep 17 00:00:00 2001 From: Goober5000 Date: Thu, 23 Nov 2023 13:11:40 -0500 Subject: [PATCH] add Lua function for movie duration This retrieves the duration from the movie stream. Thanks to @asarium for pointing me in the right direction. --- code/cutscene/Decoder.h | 1 + code/cutscene/ffmpeg/FFMPEGDecoder.cpp | 1 + code/scripting/api/objs/movie_player.cpp | 22 ++++++++++++++++++++-- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/code/cutscene/Decoder.h b/code/cutscene/Decoder.h index 2a12ad4cc0..3e501035e4 100644 --- a/code/cutscene/Decoder.h +++ b/code/cutscene/Decoder.h @@ -52,6 +52,7 @@ struct MovieProperties { FrameSize size; float fps = -1.0f; + float duration = 0.0f; FramePixelFormat pixelFormat = FramePixelFormat::Invalid; }; diff --git a/code/cutscene/ffmpeg/FFMPEGDecoder.cpp b/code/cutscene/ffmpeg/FFMPEGDecoder.cpp index bbb1a02d94..33664fc7fc 100644 --- a/code/cutscene/ffmpeg/FFMPEGDecoder.cpp +++ b/code/cutscene/ffmpeg/FFMPEGDecoder.cpp @@ -503,6 +503,7 @@ MovieProperties FFMPEGDecoder::getProperties() const props.size.height = static_cast(m_status->videoCodecPars.height); props.fps = static_cast(getFrameRate(m_status->videoStream, m_status->videoCodecCtx)); + props.duration = static_cast(m_status->videoStream->duration * av_q2d(m_status->videoStream->time_base)); props.pixelFormat = getPixelFormat(getConversionFormat(m_status->videoCodecPars.pixel_format)); diff --git a/code/scripting/api/objs/movie_player.cpp b/code/scripting/api/objs/movie_player.cpp index d450a5b1c7..576c688d8b 100644 --- a/code/scripting/api/objs/movie_player.cpp +++ b/code/scripting/api/objs/movie_player.cpp @@ -53,10 +53,10 @@ ADE_VIRTVAR(FPS, l_MoviePlayer, "number", "Determines the frames per second of t { movie_player_h* ph = nullptr; if (!ade_get_args(L, "o", l_MoviePlayer.GetPtr(&ph))) - return ade_set_args(L, "i", -1); + return ade_set_args(L, "f", -1); if (ph == nullptr || !ph->isValid()) { - return ade_set_args(L, "i", -1); + return ade_set_args(L, "f", -1); } if (ADE_SETTING_VAR) { @@ -66,6 +66,24 @@ ADE_VIRTVAR(FPS, l_MoviePlayer, "number", "Determines the frames per second of t return ade_set_args(L, "f", ph->player()->getMovieProperties().fps); } +ADE_VIRTVAR(Duration, l_MoviePlayer, "number", "Determines the duration in seconds of this movie Read-only", "number", + "The duration of the movie or -1 if handle is invalid") +{ + movie_player_h* ph = nullptr; + if (!ade_get_args(L, "o", l_MoviePlayer.GetPtr(&ph))) + return ade_set_args(L, "f", -1); + + if (ph == nullptr || !ph->isValid()) { + return ade_set_args(L, "f", -1); + } + + if (ADE_SETTING_VAR) { + LuaError(L, "Tried to modify read-only member!"); + } + + return ade_set_args(L, "f", ph->player()->getMovieProperties().duration); +} + ADE_FUNC(update, l_MoviePlayer, "timespan step_time", "Updates the current state of the movie and moves the internal timer forward by the specified time span.", "boolean", "true if there is more to display, false otherwise")