I try to read a mpeg2 video from a stream with ffmpeg library. In my first tests, I tried to read from a byte buffer, but now I read directly from a file (in my ReadPacket function) in order to be sure it's right data.
I tried many way to do that, but av_open_input_stream function return always -1.
I post my code here and hope someone could help me ! If someone have a full code to do that, i''ll be happy to see it. Thanks.
Why not use FFmpeg to open the video file? I hate giving out code, but this is what I use to open and play video files. It accepts anything FFmpeg can read, not just mpeg2video. It's a copy 'n' paste of a small portion of my code, and for my personal purposes I just needed the frame to be an 8-bit gray frame so I convert all frames to 8-bit gray. If there's a particular part you have a question about, feel free to ask. Otherwise, I'm going to assume the code is self-explanatory.
if ((errorCode = avformat_open_input(&formatContext, filename.c_str(), NULL, NULL)) != 0)
{
throw std::runtime_error("Unable to open the stream");
}
if ((errorCode = avformat_find_stream_info(formatContext, NULL)) < 0)
{
throw std::runtime_error("Unable to find the stream's info");
}
for (unsigned int i = 0; i < formatContext->nb_streams; ++i)
{
if (formatContext->streams->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
videoStreamIndex = i;
break;
}
}
if (videoStreamIndex == -1)
{
throw std::runtime_error("Unable to find a video stream");
}
if ((errorCode = avcodec_open2(codecContext, codec, NULL)) != 0)
{
throw std::runtime_error("Error opening the codec");
}
frameRate = formatContext->streams[videoStreamIndex]->avg_frame_rate;
if (!frameRate.num || ! frameRate.den)
{
throw std::runtime_error("This video does not have a constant frame rate");
}
//TODO: if one of these fail, we should release what has succeeded
frame = avcodec_alloc_frame();
grayFrame = avcodec_alloc_frame();
if (frame == NULL || grayFrame == NULL)
{
throw std::runtime_error("Unable to allocate frame memory");
}
Thanks for your reply;
I want to read from stream (in a char *buffer), but as it not work I changed my buffer by reading directly the file, that's why I use [color=#000000][size=2]
av_open_input_stream.
I have already tried to open my file with av_open_input_file and it's work, but it's not my goal here.