Using FFmpeg lastest version ("Ada") with native code and write command in android .cpp file below it's very simple function VideogetDuration() :

#include <jni.h>
#include <string>
#include <iostream>
#include <android/log.h>
#include <unistd.h>

extern "C" {
#include <libavutil/timestamp.h>
#include <libavformat/avformat.h>
#include <libavutil/display.h>

     * @param input - the absolute path to file
     * @returns the duration of file in seconds
    extern "C"
    Java_com_ffmpegjni_videoprocessinglibrary_VideoProcessing_getDuration(JNIEnv *env,
                                                                          jobject instance,
                                                                          jstring input_) {
        AVFormatContext *pFormatCtx = NULL;
        if (avformat_open_input(&pFormatCtx, jStr2str(env, input_), NULL, NULL) < 0) {
            throwException(env, "Could not open input file");
            return 0;

        if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
            throwException(env, "Failed to retrieve input stream information");
            return 0;

        int64_t duration = pFormatCtx->duration;

        return (jint) (duration / AV_TIME_BASE);

when this value returns 0 value either the wrong value. also i have read the below docs for more information. 1)http://dranger.com/ffmpeg/data.html 2)https://static.packt-cdn.com/downloads/Developing_Multimedia_Applications_with_NDK.pdf 3)https://github.com/KucherenkoIhor/VideoProcessingLibrary (also see this project) 4)https://github.com/leandromoreira/ffmpeg-libav-tutorial#video---what-you-see

i have also add the same problem with below link : https://stackoverflow.com/questions/6451814/how-to-use-libavcodec-ffmpeg-to-find-duration-of-video-file/58353968#58353968

  • Welcome! Unfortunately, this seems more like a programming question than a video production question, even though it involves ffmpeg. You might get better answers on SO. – Michael Liebman Oct 15 '19 at 23:59
  • Ok thank you @ Michael Liebman but, I think this question is related to ffmpeg_video So, i have added in SE. But, have you suggested then i'll try with SO. – axita.savani Oct 16 '19 at 4:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.