Read video file, save frame picture as ppm file

Source: Internet
Author: User

FFmpeg and SDL's learning process:
First, version information:
ffmpeg-3.0.2.tar.bz2
Sdl2-2.0.4.tar.gz
Second, the compilation process:
1, Ffmgeg of the compilation:
./configure--enable-shared--disable-yasm--prefix=/usr/local/ffmpeg
Make
Make install

2, SDL's compilation:
./configure--prefix=/usr/local/sdl
Make
Make install

3, the System environment configuration:
View/etc/ld.so.conf, known as "System shared Library Path"


Method One: The compiled SDL Dynamic library, ffmpeg dynamic library, copied to the "System shared Library path", after the replication is complete, create a soft connection.
(Call command Ldconfig after completion??? )


Method Two: Modify/etc/ld.so.conf
Append the following two lines to the ld.so.conf file
/usr/local/ffmpeg/lib
/usr/local/sdl/lib
Call the ldconfig directive.
Ldconfig do these things are related to running the program, with the compiler to connect to the SDL library, compiled connection FFmpeg library a little relationship. Compile the time or the addition-l will have to add, do not confuse the

Method Three: Temporary environment variables
Export Ld_library_path= $LD _library_path:/usr/local/ffmpeg/lib:/usr/local/sdl/lib


Method Four: Modify the shell file:
$ VI ~/.bash_profile
Without ld_library_path words, add:
Ld_library_path=/usr/local/ffmpeg/lib:/usr/local/sdl/lib
Export Ld_library_path

If there is Ld_library_path,
Add the path after Ld_library_path.
Ld_library_path=.......:/usr/local/ffmpeg/lib:/usr/local/sdl/lib

After you finish modifying the shell file, then run
$ source ~/.bash_profile on the line.
------------------------------------------------------------------
Under the shell to try to set Ld_library_path, the following form of Setup, always error Bash:LD_LIBRARY_PATH:command not found,
Ld_library_path=/usr/local/lib
Ld_library_path = $ ld_library_path:/usr/local/lib
It may be because the system has not been set up before Ld_library_path, so change to this:
Export Ld_library_path=/usr/local/lib
Then use the Echo $LD _library_path to check if it really sets the success
------------------------------------------------------------
Third, write code, call ffmpeg dynamic Library.
Code, read the video file, save the frame picture as ppm file

#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <stdio.h>

void Saveframe (Avframe *pframe, int width, int height, int iFrame)
{
FILE *pfile;
Char szfilename[32];
int y;

Open file
sprintf (szFileName, "frame%d.ppm", iFrame);
Pfile=fopen (szFileName, "WB");
if (pfile==null)
Return

Write Header
fprintf (PFile, "p6\n%d%d\n255\n", width, height);

Write pixel Data
for (y=0; yFwrite (Pframe->data[0]+y*pframe->linesize[0], 1, width*3, pFile);

Close file
Fclose (PFile);
}


int main (int argc, char* argv[])
{
Avformatcontext*pformatctx;
Inti, Videoindex;
Avcodeccontext*pcodecctx;
Avcodec*pcodec;
Av_register_all ();
Avformat_network_init ();
Pformatctx = Avformat_alloc_context ();
if (Avformat_open_input (&pformatctx,argv[1],null,null)!=0)
{
printf ("Open File error\n");
return-1;
}

if (Avformat_find_stream_info (Pformatctx,null) < 0)
{
return-1;
}

i = 0;
int videostream =-1;
printf ("pformatctx->nb_streams=%d\n", pformatctx->nb_streams);
for (i=0;i<pformatctx->nb_streams;i++)
{
if (Pformatctx->streams[i]->codec->codec_type = = Avmedia_type_video)
{
Videostream = i;
Break
}
}
printf ("videostream=%d\n", videostream);

if ( -1 = = Videostream)
{
printf ("Error no video stream\n");
Return
}

Pcodecctx = pformatctx->streams[videostream]->codec;


Pcodec = Avcodec_find_decoder (pcodecctx->codec_id);

if (NULL = = Pcodec)
{
printf ("couldn ' t find the decode\n");
return-1;
}

if (Avcodec_open2 (Pcodecctx, Pcodec, NULL) < 0)
{
printf ("Open decode error\n");
return-1;
}

Avframe*pframe,*pframergb;
Pframe = Av_frame_alloc ();
Pframergb = Av_frame_alloc ();
uint8_t *out_buffer;

int num = avpicture_get_size (av_pix_fmt_rgb24, Pcodecctx->width, pcodecctx->height);
printf ("num=%d\n", num);


Out_buffer = (uint8_t *) Av_malloc (num*sizeof (uint8_t));
Avpicture_fill ((Avpicture *) Pframergb, Out_buffer, Av_pix_fmt_rgb24, Pcodecctx->width, pCodecCtx->height);


Avpacket packet;
int ret =-1;
i = 0;
struct Swscontext *img_convert_ctx = NULL;
Img_convert_ctx = Sws_getcontext (pcodecctx->width,pcodecctx->height,pcodecctx->pix_fmt, pCodecCtx-> width, pcodecctx->height, av_pix_fmt_rgb24, sws_bicubic, NULL, NULL, NULL);
while (Av_read_frame (Pformatctx, &packet) >=0)
{
printf ("i=%d, videoindex=%d, packet.stream_index=%d\n", i++, Videoindex, Packet.stream_index);

if (Packet.stream_index = = Videostream)
{
printf ("111111\n");
int got_picture =-1;
ret = Avcodec_decode_video2 (Pcodecctx, Pframe, &got_picture, &packet);

if (Ret < 0)
{
printf ("Decode error\n");
return-1;
}

printf ("got_picture:%d\n", got_picture);
if (got_picture)
{
Sws_scale (Img_convert_ctx, Pframe->data, pframe->linesize, 0, Pcodecctx->height, PFrameRGB->data, Pframergb->linesize);
if (++i<=20)
{
Saveframe (Pframergb, Pcodecctx->width, pcodecctx->height, i);
}

}
}

Av_free_packet (&packet);
}

Free (out_buffer);
Av_free (PFRAMERGB);

Free the YUV frame
Av_free (pframe);

Close the Codec
Avcodec_close (PCODECCTX);

Close the video file
Avformat_close_input (&PFORMATCTX);


return 0;
}

Compile Instructions:
Gcc-o Test Main.c-i/usr/local/ffmpeg/include-l/usr/local/ffmpeg/lib-lavutil-lavformat-lavcodec-lswscale-lz

Sdl,ffmpeg are called by the compiler directive:
Gcc-o Test Main.c-i/usr/local/ffmpeg/include-i/usr/local/sdl/include-l/usr/local/ffmpeg/lib-l/usr/local/sdl/lib- Lavutil-lavformat-lavcodec-lswscale-lz-lsdl2

Read video file, save frame picture as ppm file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.