0

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"
    JNIEXPORT jint JNICALL
    Java_com_ffmpegjni_videoprocessinglibrary_VideoProcessing_getDuration(JNIEnv *env,
                                                                          jobject instance,
                                                                          jstring input_) {
        av_register_all();
        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;


        avformat_close_input(&pFormatCtx);
        avformat_free_context(pFormatCtx);
        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 : how to use libavcodec/ffmpeg to find duration of video file

axita.savani
  • 406
  • 1
  • 6
  • 21
  • What is your input file's format? – BrianChen Nov 15 '19 at 02:22
  • Input File format is ".mp4" Video File . @BrianChen – axita.savani Nov 15 '19 at 03:57
  • What you mean the wrong value? Would you like to print the value of duration? – BrianChen Nov 15 '19 at 04:17
  • Yes, When i run this command then it will display wrong value means the wrong duration of the video display in a millisecond. @BrianChen – axita.savani Nov 15 '19 at 05:33
  • Duration means duration of the stream, in AV_TIME_BASE fractional seconds. So if you divide duration by AV_TIME_BASE, you get a second not a millisecond and you lost precision. – BrianChen Nov 15 '19 at 05:39
  • Can you please Check Again i have write **"duration / AV_TIME_BASE"** value is return. and not match the same as video seconds. – axita.savani Nov 15 '19 at 05:41
  • You should check the video stream duration and the audio stream duration, ffmpeg deduce duration from video stream and audio stream. Maybe video player have different deduced method with ffmpeg. – BrianChen Nov 15 '19 at 05:54
  • OK. I'll check that. Thank you for the suggestion @BrianChen But, have you check that it's used different video streams by FFmpeg and VideoPlayer? – axita.savani Nov 15 '19 at 06:06
  • It is not used different video streams. The duration of video stream maybe is not equal to the duration of audio stream, we choose the greater one as file duration in general, but you also can calculate the duration by other way. – BrianChen Nov 15 '19 at 06:14
  • OK. i will check and answer you. @BrianChen Thank you. – axita.savani Nov 15 '19 at 06:19

0 Answers0