The code is as follows:
function video2bmp (const strvideofilename, strsavepath:string): Boolean;var pfc1:pavformatcontext; Pfc2:pavformatcontext; Intvideostreamindex:integer; Pps:ppavstream; Iii:integer; Pcc:pavcodeccontext; Pcodec:pavcodec; Pframevdo:pavframe; Pframergb:pavframe; Psc:pswscontext; Avp:tavpacket; Intremain:integer; Bfirst:boolean; Intdecoded:integer; Intframefinished:integer; Bmp:tbitmap;label lbexit;begin Result: = False; {1. Register all container formats and codec} av_register_all (); {2. Open file} pfc1: = nil; If Avformat_open_input (PFC1, Pansichar (ansistring (strvideofilename)), nil, nil) <> 0 THEN BEGIN ShowMessage (' Open Text Piece failed, please check your file! '); Exit; End {3. Extracting stream information from a file} if Avformat_find_stream_info (PFC1, nil) < 0 THEN begin ShowMessage (' no audio or video streaming! '); Exit; End {4. Exhaustive all streams, find if video stream is included} Intvideostreamindex: =-1; PFC2: = PFC1; PPS: = Pfc1^.streams; For III: = 0 to Pfc1^.nb_streams-1 does begin if Pps^.codec.codec_type = Avmedia_type_video THEN BEGIN Intvideostreamindex: = III; End Inc (PPS); End If Intvideostreamindex =-1 THEN BEGIN ShowMessage (' There is no video stream! '); Exit; End {5. Navigate to the Video Stream} Inc (Pfc1^.streams, Intvideostreamindex); PCC: = Pfc1^.streams^.codec; Pcodec: = Avcodec_find_decoder (pcc^.codec_id); If Pcodec = nil then begin ShowMessage (' Can't find video stream! '); Exit; End {6. Fix video Stream} if (Pcodec^.capabilities and codec_cap_truncated) = Codec_cap_truncated then pcc^.flags: = Pcc.flags or C odec_flag_truncated; If Avcodec_open2 (PCC, Pcodec, nil) < 0 THEN begin ShowMessage (' Open video stream error! '); Exit; End {7. The video stream is converted to BMP} if (Pcc^.time_base.num >) and (Pcc^.time_base.den = 1) then Pcc^.time_base.den: = 1000; PFRAMEVDO: = Avcodec_alloc_frame (); Pframergb: = Avcodec_alloc_frame; {Output A picture by the original size of the video frame} avPicture_alloc (Pavpicture (PFRAMERGB), Av_pix_fmt_rgb32, pcc^. Width, pcc^. Height); PSC: = Sws_getcontext (pcc^. Width, pcc^. Height, Pcc^.pix_fmt, pcc^. Width, pcc^. Height, Av_pix_fmt_rgb32, sws_bicubic, nil, nil, nil); Intremain: = 0; Bfirst: = True; {Look for each frame of video} while True does begin if Bfirst then BEGIN bfirst: = False; Avp.data: = nil; End While True does begin while Intremain > 0 does begin intdecoded: = Avcodec_decode_video2 (PCC, PFRAMEVDO , intframefinished, @avp); If intdecoded < 0 then break; Dec (Intremain, intdecoded); {Find the full frame} if intframefinished <> 0 THEN begin {output by original} Sws_scale (PSC, @pFrameVdo ^ . Data, @pFrameVdo ^.linesize, 0, pcc^. Height, @pFrameRGB ^.data, @pFrameRGB ^.linesize); {Output bitmap} BMP: = Tbitmap.create; Try BMP. PixelFormat: = Pf32bit; Bmp. Width: = pcc^. Width; Bmp. Height: = pcc^. HEight For-III: = 0 to BMP. Height-1 do begin CopyMemory (BMP. SCANLINE[III], Pointer (Integer (pframergb.data[0]) + BMP. Width * 4 * III), BMP. Width * 4); End Bmp. SaveToFile (Format ('%s\%d.bmp ', [Strsavepath, GetTickCount])); Finally Bmp.free; End {Looking for Next frame} Continue; End End Repeat if Av_read_frame (PFC2, @avp) < 0 then Goto Lbexit; Until Avp.stream_index = Intvideostreamindex; Intremain: = avp.size; End Lbexit:avcodec_decode_video2 (PCC, PFRAMEVDO, intframefinished, @avp); If Avp.data <> nil then av_free_packet (@avp); If intframefinished = 0 then break; End {8. Releasing resources} sws_freecontext (PSC); Avpicture_free (Pavpicture (PFRAMERGB)); Av_free (PFRAMERGB); Av_free (PFRAMEVDO); Avcodec_close (PCC); Avformat_close_input (PFC1); {9. Return success} Result: = True;end;
Share a piece of code, convert videos based on ffmpeg to pictures