avcodec library not extracting all the frames from a video











up vote
0
down vote

favorite












I have written a library that could be used with python to extract frames from a video in a buffer for some processing. I used the examples from the ffmpeg to build my library using libavcodec.
This and seems to be working fine for most of the videos. But I see on some videos where my library does not extract all the frames (way less than the FPS).



It seems like somehow the packets might be out of order and docoder is not able to handle it. I keep getting a lot of the following errors




pid=100 pes_code=0x1e0



nal_unit_type: 9, nal_ref_idc: 0



nal_unit_type: 1, nal_ref_idc: 2



deblocking_filter_idc 7 out of range



decode_slice_header error



no frame!




I use the following steps to setup the decoder.



    //open input file, allocate context
if ((ret = avformat_open_input(&format_ctx, in_filename, 0, 0)) < 0) {
PyErr_SetString(ExtractorError, "Could not open input file!");
goto end;
}

if ((ret = avformat_find_stream_info(format_ctx, 0)) < 0) {
PyErr_SetString(ExtractorError, "Failed to retrieve input stream information!");
goto end;
}

av_dump_format(format_ctx, 0, in_filename, 0);

// Get the video index from the stream
for(int i=0; i<format_ctx->nb_streams ;i++ )
{
if( format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO )
{
video_stream_index = i;
break;
}
}

/* if video stream not availabe */
if((video_stream_index) == -1)
{
PyErr_SetString(ExtractorError, "video stream to retreive fps is not found!");
return NULL;
}

long duration = format_ctx->duration + (format_ctx->duration <= INT64_MAX - 5000 ? 5000 : 0);

float duration_in_secs = (float)duration / AV_TIME_BASE;

stream_mapping_size = format_ctx->nb_streams;
stream_mapping = av_mallocz_array(stream_mapping_size, sizeof(*stream_mapping));
if (!stream_mapping) {
ret = AVERROR(ENOMEM);
goto end;
}

AVCodec *pCodec = NULL;
AVCodecParameters *in_codecpar = NULL;
for (i = 0; i < format_ctx->nb_streams; i++) {
AVStream *in_stream = format_ctx->streams[i];
in_codecpar = in_stream->codecpar;

if (in_codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&
in_codecpar->codec_type != AVMEDIA_TYPE_VIDEO &&
in_codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) {
stream_mapping[i] = -1;
continue;
}

if (in_codecpar->codec_type == AVMEDIA_TYPE_VIDEO){
pCodec = avcodec_find_decoder(in_codecpar->codec_id);
stream_mapping[i] = stream_index++;
break;
}
}

if(!pCodec){
PyErr_SetString(ExtractorError, "error, no pCodec!");
return NULL;
}

// convert CodecParam to CodecCtx
AVCodecContext *pCodecCtx = avcodec_alloc_context3(pCodec);
if (!pCodecCtx) {
PyErr_SetString(ExtractorError, "Failed to convert codecParam to CodecCtx!");
return NULL;
}

ret = avcodec_parameters_to_context(pCodecCtx, in_codecpar);
if (ret < 0)
goto end;

//open video decoder
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
{
logging("EXTRACTOR: failed to open codec through avcodec_open2n");
return NULL;
}


In order to test it I used the following command from ffmpeg to extract frames from the same video and it worked fine.




ffmpeg -i ~/thuuz/extractor/03000.ts -f image2 -qscale:v 7 ffmpeg-detect--%03d.jpg -hide_banner -v quiet




Am I not passing the right codec params? Please let me know some inputs on how to debug this issue.
Thanks a lot.










share|improve this question













migrated from superuser.com Nov 22 at 7:14


This question came from our site for computer enthusiasts and power users.















  • you posted the code to open the clip but you have problems decoding the stream... Are you sure that your problem is with the opening process? Does it work with the unmodified examples of ffmpeg?
    – Harry
    Nov 23 at 22:38















up vote
0
down vote

favorite












I have written a library that could be used with python to extract frames from a video in a buffer for some processing. I used the examples from the ffmpeg to build my library using libavcodec.
This and seems to be working fine for most of the videos. But I see on some videos where my library does not extract all the frames (way less than the FPS).



It seems like somehow the packets might be out of order and docoder is not able to handle it. I keep getting a lot of the following errors




pid=100 pes_code=0x1e0



nal_unit_type: 9, nal_ref_idc: 0



nal_unit_type: 1, nal_ref_idc: 2



deblocking_filter_idc 7 out of range



decode_slice_header error



no frame!




I use the following steps to setup the decoder.



    //open input file, allocate context
if ((ret = avformat_open_input(&format_ctx, in_filename, 0, 0)) < 0) {
PyErr_SetString(ExtractorError, "Could not open input file!");
goto end;
}

if ((ret = avformat_find_stream_info(format_ctx, 0)) < 0) {
PyErr_SetString(ExtractorError, "Failed to retrieve input stream information!");
goto end;
}

av_dump_format(format_ctx, 0, in_filename, 0);

// Get the video index from the stream
for(int i=0; i<format_ctx->nb_streams ;i++ )
{
if( format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO )
{
video_stream_index = i;
break;
}
}

/* if video stream not availabe */
if((video_stream_index) == -1)
{
PyErr_SetString(ExtractorError, "video stream to retreive fps is not found!");
return NULL;
}

long duration = format_ctx->duration + (format_ctx->duration <= INT64_MAX - 5000 ? 5000 : 0);

float duration_in_secs = (float)duration / AV_TIME_BASE;

stream_mapping_size = format_ctx->nb_streams;
stream_mapping = av_mallocz_array(stream_mapping_size, sizeof(*stream_mapping));
if (!stream_mapping) {
ret = AVERROR(ENOMEM);
goto end;
}

AVCodec *pCodec = NULL;
AVCodecParameters *in_codecpar = NULL;
for (i = 0; i < format_ctx->nb_streams; i++) {
AVStream *in_stream = format_ctx->streams[i];
in_codecpar = in_stream->codecpar;

if (in_codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&
in_codecpar->codec_type != AVMEDIA_TYPE_VIDEO &&
in_codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) {
stream_mapping[i] = -1;
continue;
}

if (in_codecpar->codec_type == AVMEDIA_TYPE_VIDEO){
pCodec = avcodec_find_decoder(in_codecpar->codec_id);
stream_mapping[i] = stream_index++;
break;
}
}

if(!pCodec){
PyErr_SetString(ExtractorError, "error, no pCodec!");
return NULL;
}

// convert CodecParam to CodecCtx
AVCodecContext *pCodecCtx = avcodec_alloc_context3(pCodec);
if (!pCodecCtx) {
PyErr_SetString(ExtractorError, "Failed to convert codecParam to CodecCtx!");
return NULL;
}

ret = avcodec_parameters_to_context(pCodecCtx, in_codecpar);
if (ret < 0)
goto end;

//open video decoder
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
{
logging("EXTRACTOR: failed to open codec through avcodec_open2n");
return NULL;
}


In order to test it I used the following command from ffmpeg to extract frames from the same video and it worked fine.




ffmpeg -i ~/thuuz/extractor/03000.ts -f image2 -qscale:v 7 ffmpeg-detect--%03d.jpg -hide_banner -v quiet




Am I not passing the right codec params? Please let me know some inputs on how to debug this issue.
Thanks a lot.










share|improve this question













migrated from superuser.com Nov 22 at 7:14


This question came from our site for computer enthusiasts and power users.















  • you posted the code to open the clip but you have problems decoding the stream... Are you sure that your problem is with the opening process? Does it work with the unmodified examples of ffmpeg?
    – Harry
    Nov 23 at 22:38













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have written a library that could be used with python to extract frames from a video in a buffer for some processing. I used the examples from the ffmpeg to build my library using libavcodec.
This and seems to be working fine for most of the videos. But I see on some videos where my library does not extract all the frames (way less than the FPS).



It seems like somehow the packets might be out of order and docoder is not able to handle it. I keep getting a lot of the following errors




pid=100 pes_code=0x1e0



nal_unit_type: 9, nal_ref_idc: 0



nal_unit_type: 1, nal_ref_idc: 2



deblocking_filter_idc 7 out of range



decode_slice_header error



no frame!




I use the following steps to setup the decoder.



    //open input file, allocate context
if ((ret = avformat_open_input(&format_ctx, in_filename, 0, 0)) < 0) {
PyErr_SetString(ExtractorError, "Could not open input file!");
goto end;
}

if ((ret = avformat_find_stream_info(format_ctx, 0)) < 0) {
PyErr_SetString(ExtractorError, "Failed to retrieve input stream information!");
goto end;
}

av_dump_format(format_ctx, 0, in_filename, 0);

// Get the video index from the stream
for(int i=0; i<format_ctx->nb_streams ;i++ )
{
if( format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO )
{
video_stream_index = i;
break;
}
}

/* if video stream not availabe */
if((video_stream_index) == -1)
{
PyErr_SetString(ExtractorError, "video stream to retreive fps is not found!");
return NULL;
}

long duration = format_ctx->duration + (format_ctx->duration <= INT64_MAX - 5000 ? 5000 : 0);

float duration_in_secs = (float)duration / AV_TIME_BASE;

stream_mapping_size = format_ctx->nb_streams;
stream_mapping = av_mallocz_array(stream_mapping_size, sizeof(*stream_mapping));
if (!stream_mapping) {
ret = AVERROR(ENOMEM);
goto end;
}

AVCodec *pCodec = NULL;
AVCodecParameters *in_codecpar = NULL;
for (i = 0; i < format_ctx->nb_streams; i++) {
AVStream *in_stream = format_ctx->streams[i];
in_codecpar = in_stream->codecpar;

if (in_codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&
in_codecpar->codec_type != AVMEDIA_TYPE_VIDEO &&
in_codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) {
stream_mapping[i] = -1;
continue;
}

if (in_codecpar->codec_type == AVMEDIA_TYPE_VIDEO){
pCodec = avcodec_find_decoder(in_codecpar->codec_id);
stream_mapping[i] = stream_index++;
break;
}
}

if(!pCodec){
PyErr_SetString(ExtractorError, "error, no pCodec!");
return NULL;
}

// convert CodecParam to CodecCtx
AVCodecContext *pCodecCtx = avcodec_alloc_context3(pCodec);
if (!pCodecCtx) {
PyErr_SetString(ExtractorError, "Failed to convert codecParam to CodecCtx!");
return NULL;
}

ret = avcodec_parameters_to_context(pCodecCtx, in_codecpar);
if (ret < 0)
goto end;

//open video decoder
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
{
logging("EXTRACTOR: failed to open codec through avcodec_open2n");
return NULL;
}


In order to test it I used the following command from ffmpeg to extract frames from the same video and it worked fine.




ffmpeg -i ~/thuuz/extractor/03000.ts -f image2 -qscale:v 7 ffmpeg-detect--%03d.jpg -hide_banner -v quiet




Am I not passing the right codec params? Please let me know some inputs on how to debug this issue.
Thanks a lot.










share|improve this question













I have written a library that could be used with python to extract frames from a video in a buffer for some processing. I used the examples from the ffmpeg to build my library using libavcodec.
This and seems to be working fine for most of the videos. But I see on some videos where my library does not extract all the frames (way less than the FPS).



It seems like somehow the packets might be out of order and docoder is not able to handle it. I keep getting a lot of the following errors




pid=100 pes_code=0x1e0



nal_unit_type: 9, nal_ref_idc: 0



nal_unit_type: 1, nal_ref_idc: 2



deblocking_filter_idc 7 out of range



decode_slice_header error



no frame!




I use the following steps to setup the decoder.



    //open input file, allocate context
if ((ret = avformat_open_input(&format_ctx, in_filename, 0, 0)) < 0) {
PyErr_SetString(ExtractorError, "Could not open input file!");
goto end;
}

if ((ret = avformat_find_stream_info(format_ctx, 0)) < 0) {
PyErr_SetString(ExtractorError, "Failed to retrieve input stream information!");
goto end;
}

av_dump_format(format_ctx, 0, in_filename, 0);

// Get the video index from the stream
for(int i=0; i<format_ctx->nb_streams ;i++ )
{
if( format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO )
{
video_stream_index = i;
break;
}
}

/* if video stream not availabe */
if((video_stream_index) == -1)
{
PyErr_SetString(ExtractorError, "video stream to retreive fps is not found!");
return NULL;
}

long duration = format_ctx->duration + (format_ctx->duration <= INT64_MAX - 5000 ? 5000 : 0);

float duration_in_secs = (float)duration / AV_TIME_BASE;

stream_mapping_size = format_ctx->nb_streams;
stream_mapping = av_mallocz_array(stream_mapping_size, sizeof(*stream_mapping));
if (!stream_mapping) {
ret = AVERROR(ENOMEM);
goto end;
}

AVCodec *pCodec = NULL;
AVCodecParameters *in_codecpar = NULL;
for (i = 0; i < format_ctx->nb_streams; i++) {
AVStream *in_stream = format_ctx->streams[i];
in_codecpar = in_stream->codecpar;

if (in_codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&
in_codecpar->codec_type != AVMEDIA_TYPE_VIDEO &&
in_codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) {
stream_mapping[i] = -1;
continue;
}

if (in_codecpar->codec_type == AVMEDIA_TYPE_VIDEO){
pCodec = avcodec_find_decoder(in_codecpar->codec_id);
stream_mapping[i] = stream_index++;
break;
}
}

if(!pCodec){
PyErr_SetString(ExtractorError, "error, no pCodec!");
return NULL;
}

// convert CodecParam to CodecCtx
AVCodecContext *pCodecCtx = avcodec_alloc_context3(pCodec);
if (!pCodecCtx) {
PyErr_SetString(ExtractorError, "Failed to convert codecParam to CodecCtx!");
return NULL;
}

ret = avcodec_parameters_to_context(pCodecCtx, in_codecpar);
if (ret < 0)
goto end;

//open video decoder
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
{
logging("EXTRACTOR: failed to open codec through avcodec_open2n");
return NULL;
}


In order to test it I used the following command from ffmpeg to extract frames from the same video and it worked fine.




ffmpeg -i ~/thuuz/extractor/03000.ts -f image2 -qscale:v 7 ffmpeg-detect--%03d.jpg -hide_banner -v quiet




Am I not passing the right codec params? Please let me know some inputs on how to debug this issue.
Thanks a lot.







linux video ffmpeg video-encoding video-codecs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 23:17









Guru Govindan

7941820




7941820




migrated from superuser.com Nov 22 at 7:14


This question came from our site for computer enthusiasts and power users.






migrated from superuser.com Nov 22 at 7:14


This question came from our site for computer enthusiasts and power users.














  • you posted the code to open the clip but you have problems decoding the stream... Are you sure that your problem is with the opening process? Does it work with the unmodified examples of ffmpeg?
    – Harry
    Nov 23 at 22:38


















  • you posted the code to open the clip but you have problems decoding the stream... Are you sure that your problem is with the opening process? Does it work with the unmodified examples of ffmpeg?
    – Harry
    Nov 23 at 22:38
















you posted the code to open the clip but you have problems decoding the stream... Are you sure that your problem is with the opening process? Does it work with the unmodified examples of ffmpeg?
– Harry
Nov 23 at 22:38




you posted the code to open the clip but you have problems decoding the stream... Are you sure that your problem is with the opening process? Does it work with the unmodified examples of ffmpeg?
– Harry
Nov 23 at 22:38

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425636%2favcodec-library-not-extracting-all-the-frames-from-a-video%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425636%2favcodec-library-not-extracting-all-the-frames-from-a-video%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

AnyDesk - Fatal Program Failure

QoS: MAC-Priority for clients behind a repeater

Актюбинская область