how to get video duration

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

how to get video duration

kilamski
Hi,

I am using following example to get an* mpeg-TS video file duration:*
https://gstreamer.freedesktop.org/documentation/tutorials/basic/media-information-gathering.html?gi-language=c
However this code is 200 lines long. Is there a more elegant and short way
to do it.
Right click on Ubuntu will also give me video duration but I have no idea
how to do this programatically.
Thanks for any hints.



--
Sent from: http://gstreamer-devel.966125.n4.nabble.com/
_______________________________________________
gstreamer-devel mailing list
[hidden email]
https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel
Reply | Threaded
Open this post in threaded view
|

Re: how to get video duration

Nicolas Dufresne-5
Le jeudi 20 juin 2019 à 10:50 -0500, kilamski a écrit :
> Hi,
>
> I am using following example to get an* mpeg-TS video file duration:*
> https://gstreamer.freedesktop.org/documentation/tutorials/basic/media-information-gathering.html?gi-language=c
> However this code is 200 lines long. Is there a more elegant and short way
> to do it.
> Right click on Ubuntu will also give me video duration but I have no idea
> how to do this programatically.
> Thanks for any hints.

If you are programming a cli, where blocking is fine, something like
this should do (untested):

g_autoptr(GError) error = NULL;
g_autoptr(GstDiscoverer) disc = NULL;
g_autoptr(GstDiscovererInfo) info = NULL;
GstClocktime duration;

dis = gst_discoverer_new (30 * GST_SECOND, &error);
g_assert_no_error (error);
info = gst_discoverer_discover_uri (dis, "http://myuri", &error);
g_assert_no_error (error);

duration = gst_discoverer_info_get_duration (info);

>
>
>
> --
> Sent from: http://gstreamer-devel.966125.n4.nabble.com/
> _______________________________________________
> gstreamer-devel mailing list
> [hidden email]
> https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel

_______________________________________________
gstreamer-devel mailing list
[hidden email]
https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel

signature.asc (201 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: how to get video duration

kilamski
Thank you for the feedback Nicolas,

i finally  managed to do it with a code that  is similar to the one you
attached.
Here is it is for completeness:

int get_ts_file_duration(char *path)
{
    GstDiscoverer *discoverer;
    GError *err = NULL;
    gchar *uri;
    unsigned long duration;

    uri = gst_filename_to_uri (path, &err);
    if (err)
    {
        g_print ("Error in gst_filename_to_uri(). file: %s: %s\n", path,
err->message);
        g_error_free (err);
        return -1;
    }

    /* Initialize GStreamer */
    gst_init (NULL, NULL);

    /* Instantiate the Discoverer */
    discoverer = gst_discoverer_new (1 * GST_SECOND, &err);
    if (!discoverer)
    {
        g_print ("Error creating discoverer instance: %s\n", err->message);
        g_error_free (err);
        return -1;
    }

    GstDiscovererInfo *info = gst_discoverer_discover_uri(discoverer, uri,
&err);
    if (err)
    {
        g_print ("Seems file: %s is brocken: %s\n", uri,  err->message);
        g_error_free (err);
        return -1;
    }

    /* If we got no error, show the retrieved information */
    duration = gst_discoverer_info_get_duration (info);
    duration /= 1000000000;//duration is in nanoseconds

    /* Free resources */
    g_object_unref (discoverer);
    gst_discoverer_info_unref(info);
    g_free(uri);

    return (int)duration;
}



--
Sent from: http://gstreamer-devel.966125.n4.nabble.com/
_______________________________________________
gstreamer-devel mailing list
[hidden email]
https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel