AV 格式库 avformat_free_context
AVFormat lib avformat_free_context
我尝试使用此代码来获取媒体文件的持续时间:
AVFormatContext* pFormatCtx = NULL;
avformat_open_input(&pFormatCtx, filename.c_str(), NULL, NULL);
if (pFormatCtx != 0) {
avformat_find_stream_info(pFormatCtx, NULL);
int64_t media_duration = pFormatCtx->duration;
duration = media_duration/AV_TIME_BASE;
avformat_close_input(&pFormatCtx);
avformat_free_context(pFormatCtx);
}
问题是如果我调用 avformat_free_context() 应用程序死机,如果我注释掉程序运行的行,但我更愿意避免内存泄漏。
PS:我在程序启动时调用 av_register_all()。
因为 avformat_close_input
会在 AVFormatContext
关闭后释放它。这就是为什么你应该通过指针发送 pFormatCtx
。
因此您无需调用 avformat_free_context
,因为它已被 avformat_close_input
调用,无需担心内存泄漏。
请参阅 avformat_close_input
的文档以供参考。
这个功能在我的项目中有效,试试这个。
*
* @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);
}
我尝试使用此代码来获取媒体文件的持续时间:
AVFormatContext* pFormatCtx = NULL;
avformat_open_input(&pFormatCtx, filename.c_str(), NULL, NULL);
if (pFormatCtx != 0) {
avformat_find_stream_info(pFormatCtx, NULL);
int64_t media_duration = pFormatCtx->duration;
duration = media_duration/AV_TIME_BASE;
avformat_close_input(&pFormatCtx);
avformat_free_context(pFormatCtx);
}
问题是如果我调用 avformat_free_context() 应用程序死机,如果我注释掉程序运行的行,但我更愿意避免内存泄漏。
PS:我在程序启动时调用 av_register_all()。
因为 avformat_close_input
会在 AVFormatContext
关闭后释放它。这就是为什么你应该通过指针发送 pFormatCtx
。
因此您无需调用 avformat_free_context
,因为它已被 avformat_close_input
调用,无需担心内存泄漏。
请参阅 avformat_close_input
的文档以供参考。
这个功能在我的项目中有效,试试这个。
*
* @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);
}