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

<pre><code>#include &lt;jni.h&gt;
#include &lt;string&gt;
#include &lt;iostream&gt;
#include &lt;android/log.h&gt;
#include &lt;unistd.h&gt;

extern "C" {
#include &lt;libavutil/timestamp.h&gt;
#include &lt;libavformat/avformat.h&gt;
#include &lt;libavutil/display.h&gt;
}

    /***
     *
     * @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(&amp;pFormatCtx, jStr2str(env, input_), NULL, NULL) &lt; 0) {
            throwException(env, "Could not open input file");
            return 0;
        }


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

        int64_t duration = pFormatCtx-&gt;duration;


        avformat_close_input(&amp;pFormatCtx);
        avformat_free_context(pFormatCtx);
        return (jint) (duration / AV_TIME_BASE);
    }
</code></pre>

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

<p><strong>i have also add the same problem with below link :</strong> 
<a href="https://stackoverflow.com/questions/6451814/how-to-use-libavcodec-ffmpeg-to-find-duration-of-video-file/58353968#58353968">https://stackoverflow.com/questions/6451814/how-to-use-libavcodec-ffmpeg-to-find-duration-of-video-file/58353968#58353968</a></p>

CC BY-SA 4.0
2019-10-15T23:59:39.833Z
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
CC BY-SA 4.0
2019-10-16T04:29:58.620Z
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
<ffmpeg><android>
How to write command in android when I m using FFmpeg Native with C++ file?
axita.savani
# How to write command in android when I m using FFmpeg Native with C++ file?

(Asked by: username_0 on 2019-10-15)

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

extern "C" {
#include 
#include 
#include 
}

    /***
     *
     * @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 :
https://stackoverflow.com/questions/6451814/how-to-use-libavcodec-ffmpeg-to-find-duration-of-video-file/58353968#58353968

**Tags:** ffmpeg, android

> **Comments:**
> **username_1**: 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.  
> **username_0**: Ok thank you @ username_1 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.  

---

## 0 answers

