how to use libavcodec/ffmpeg to find duration of video file

前端 未结 4 2044
半阙折子戏
半阙折子戏 2020-12-28 17:31

I needed a library to perform basic functions such as length, size, etc of a video file (i\'m guessing through the metadata or tags) so I chose ffmpeg. Vali

4条回答
  •  灰色年华
    2020-12-28 17:57

    used this function its working :

    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 I m using (jint) (duration / AV_TIME_BASE) this video duration is getting wrong.

提交回复
热议问题