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