Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install ffmpeg in Ubuntu16.04

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about how to install ffmpeg in Ubuntu16.04. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

I. ffmpeg installation

Step 1: add a source.

Sudo add-apt-repository ppa:djcj/hybrid

Step 2: update the source.

Sudo apt-get update

Step 3: download and install.

Sudo apt-get install ffmpeg

Step 4: verify.

Sudo ffmpeg-version II. C language calls ffmpeg library to parse rtsp video stream and store it as ppm format picture.

1. Reference:

Https://stackoverflow.com/questions/10715170/receiving-rtsp-stream-using-ffmpeg-library

two。 Code: my_streamer.cpp

# include # include extern "C" {# include # include} int main (int argc, char** argv) {/ / Open the initial context variables that are needed SwsContext * img_convert_ctx; AVFormatContext* format_ctx = avformat_alloc_context (); AVCodecContext* codec_ctx = NULL; int video_stream_index; / / Register everything av_register_all (); avformat_network_init () / / open RTSP if (avformat_open_input (& format_ctx, "rtsp://134.169.178.187:8554/h364.3gp", NULL, NULL)! = 0) {return EXIT_FAILURE;} if (avformat_find_stream_info (format_ctx, NULL)

< 0) { return EXIT_FAILURE; } //search video stream for (int i = 0; i < format_ctx->

Nb_streams; iTunes +) {if (format_ctx- > streams [I]-> codec- > codec_type = = AVMEDIA_TYPE_VIDEO) video_stream_index = I;} AVPacket packet; av_init_packet (& packet); / / open output file AVFormatContext* output_ctx = avformat_alloc_context (); AVStream* stream = NULL; int cnt = 0; / / start reading packets from stream and write them to file av_read_play (format_ctx) / / play RTSP / / Get the codec AVCodec * codec = NULL; codec = avcodec_find_decoder (AV_CODEC_ID_H264); if (! codec) {exit (1);} / / Add this to allocate the context by codec codec_ctx = avcodec_alloc_context3 (codec); avcodec_get_context_defaults3 (codec_ctx, codec); avcodec_copy_context (codec_ctx, format_ctx- > streams [video _ stream_index]-> codec) Std::ofstream output_file; if (avcodec_open2 (codec_ctx, codec, NULL)

< 0) exit(1); img_convert_ctx = sws_getContext(codec_ctx->

Width, codec_ctx- > height, codec_ctx- > pix_fmt, codec_ctx- > width, codec_ctx- > height, AV_PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL); int size = avpicture_get_size (AV_PIX_FMT_YUV420P, codec_ctx- > width, codec_ctx- > height); uint8_t* picture_buffer = (uint8_t*) (av_malloc (size)) AVFrame* picture = av_frame_alloc (); AVFrame* picture_rgb = av_frame_alloc (); int size2 = avpicture_get_size (AV_PIX_FMT_RGB24, codec_ctx- > width, codec_ctx- > height); uint8_t* picture_buffer_2 = (uint8_t*) (av_malloc (size2)) Avpicture_fill ((AVPicture *) picture, picture_buffer, AV_PIX_FMT_YUV420P, codec_ctx- > width, codec_ctx- > height); avpicture_fill ((AVPicture *) picture_rgb, picture_buffer_2, AV_PIX_FMT_RGB24, codec_ctx- > width, codec_ctx- > height); while (av_read_frame (format_ctx, & packet) > = 0 & & cnt

< 1000) { //read ~ 1000 frames std::cout sample_aspect_ratio = format_ctx->

Streams [video _ stream_index]-> codec- > sample_aspect_ratio;} int check = 0; packet.stream_index = stream- > id; std::cout codec_type = = AVMEDIA_TYPE_VIDEO) video_stream_index = I;} AVPacket packet; av_init_packet (& packet); / / open output file AVFormatContext* output_ctx = avformat_alloc_context () AVStream* stream = NULL; int cnt = 0; / / start reading packets from stream and write them to file av_read_play (format_ctx); / / play RTSP / / Get the codec AVCodec * codec = NULL; codec = avcodec_find_decoder (AV_CODEC_ID_H264); if (! codec) {exit (1);} / / Add this to allocate the context by codec codec_ctx = avcodec_alloc_context3 (codec) Avcodec_get_context_defaults3 (codec_ctx, codec); avcodec_copy_context (codec_ctx, format_ctx- > streams [video _ stream_index]-> codec); std::ofstream output_file; if (codec_ctx, codec, NULL)

< 0) exit(1); img_convert_ctx = sws_getContext(codec_ctx->

Width, codec_ctx- > height, codec_ctx- > pix_fmt, codec_ctx- > width, codec_ctx- > height, AV_PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL); int size = avpicture_get_size (AV_PIX_FMT_YUV420P, codec_ctx- > width, codec_ctx- > height); uint8_t* picture_buffer = (uint8_t*) (av_malloc (size)) AVFrame* picture = av_frame_alloc (); AVFrame* picture_rgb = av_frame_alloc (); int size2 = avpicture_get_size (AV_PIX_FMT_RGB24, codec_ctx- > width, codec_ctx- > height); uint8_t* picture_buffer_2 = (uint8_t*) (av_malloc (size2)) Avpicture_fill ((AVPicture *) picture, picture_buffer, AV_PIX_FMT_YUV420P, codec_ctx- > width, codec_ctx- > height); avpicture_fill ((AVPicture *) picture_rgb, picture_buffer_2, AV_PIX_FMT_RGB24, codec_ctx- > width, codec_ctx- > height); / / geyg AVFrame * pictureRGB; int numBytes; uint8_t * buffer Int iTun0; long prepts = 0; pictureRGB=avcodec_alloc_frame (); if (pictureRGB==NULL) return-1; / / Determine required buffer size and allocate buffer numBytes=avpicture_get_size (PIX_FMT_RGB24, codec_ctx- > width, codec_ctx- > height); buffer= (uint8_t *) av_malloc (numBytes*sizeof (uint8_t)) / / Assign appropriate parts of buffer to image planes in pFrameRGB avpicture_fill ((AVPicture *) pictureRGB, buffer, PIX_FMT_RGB24, codec_ctx- > width, codec_ctx- > height); while (av_read_frame (format_ctx, & packet) > = 0 & & cnt

< 1000) { //read ~ 1000 frames std::cout sample_aspect_ratio = format_ctx->

Streams [video _ stream_index]-> codec- > sample_aspect_ratio;} int check = 0; packet.stream_index = stream- > id; std::cout height, pictureRGB- > data, pictureRGB- > linesize); / / Save the frame to disk SaveFrame_jpg_uselibjpeg (pictureRGB, codec_ctx- > width, codec_ctx- > height, cnt) CopyDate (pictureRGB, codec_ctx- > width, codec_ctx- > height,packet.pts-prepts); prepts = packet.pts;} / * if (cnt > 100) / / cnt

< 0) { sws_scale(img_convert_ctx, picture->

Data, picture- > linesize, 0, codec_ctx- > height, picture_rgb- > data, picture_rgb- > linesize); std::stringstream file_name; file_name linesize [0] + j*nChannels+1]; pData [i*stepWidth+j*nChannels+2] = picture- > data [0] [iTunes Picture-> linesize [0] + j*nChannels+0] } cv::namedWindow ("Video", cv::WINDOW_NORMAL); cv::imshow ("Video", frameImage); cv::waitKey (1);} static void SaveFrame_mmp (AVFrame * picture, 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) {printf ("% s\ n", "create file fail!"); return;} / / Write header fprintf (pFile, "P6\ n% d\ n255\ n", width, height) / / Write pixel data for (yellow0; ydata [0] + yearly picture-> linesize [0], 1, width*3, pFile); / / Close file fclose (pFile);} static void SaveFrame_jpg_uselibjpeg (AVFrame* pFrame, int width, int height, int iFrame) {char fname [128] = {0}; struct jpeg_compress_struct cinfo; struct jpeg_error_mgr jerr; JSAMPROW row_pointer [1] Int row_stride; uint8_t * buffer; FILE * fp; buffer = pFrame- > data [0]; sprintf (fname, "% s%d.jpg", "frame", iFrame); fp = fopen (fname, "wb"); cinfo.err = jpeg_std_error (& jerr); jpeg_create_compress (& cinfo); jpeg_stdio_dest (& cinfo, fp) Cinfo.image_width = width; cinfo.image_height = height; cinfo.input_components = 3; cinfo.in_color_space = JCS_RGB; jpeg_set_defaults (& cinfo); jpeg_set_quality (& cinfo, 80, true); jpeg_start_compress (& cinfo, TRUE); row_stride = width * 3 While (cinfo.next_scanline < height) {row_pointer [0] = & buffer [cinfo.next _ scanline * row_stride]; (void) jpeg_write_scanlines (& cinfo, row_pointer, 1);} jpeg_finish_compress (& cinfo); fclose (fp); jpeg_destroy_compress (& cinfo); return;}

two。 Dependent library installation

1. Install opencv: user displays zmq installation under 2.apt install libavformat-devapt install libavcodec-devapt install libswresample-devapt install libswscale-devapt install libavutil-devapt install libjpeg-dev3.sudo apt-get install libsdl1.2-devsudo apt-get install libsdl-image1.2-devsudo apt-get install libsdl-mixer1.2-devsudo apt-get install libsdl-ttf2.0-devsudo apt-get install libsdl-gfx1.2-dev4.ubuntu (1) download zmq:wget http://download.zeromq.org/zeromq-4.1. 4.tar.gz (can change "4.1.4" to the current latest version number) (2) decompress: tar-zxvf zeromq-4.1.4.tar.gz (3) compile and install A. Error in executing configure file:. / configure: configure: error: Package requirements (libsodium) were not met: No package 'libsodium' found solution: ignore this library. / configure-- prefix=/home/ygy/zmq-- without-libsodium (the path in prefix is the directory where zmq is stored) B. Compilation: makeC. Installation: make installD. Configure the environment variable vi / etc/profile export clustering included path = "$C_INCLUDE_PATH:/usr/local/include" export CPLUS_INCLUDE_PATH= "$CPLUS_INCLUDE_PATH:/usr/local/include" export LD_LIBRARY_PATH= "$LD_LIBRARY_PATH:/usr/local/lib" export LIBRARY_PATH= "$LIBRARY_PATH:/usr/local/lib" C language zmq use reference: # include

3. Compile

Pkg-config +-w my_streamer.cpp-o my_streamer $(pkg-config-- cflags-- libs libavformat libswscale libavcodec libavutil opencv libjpeg libzmq)

4. Running

. / my_ stream IV. C language calls ffmpeg library to realize local video playback

two。 Dependent library installation: see section II. 3 above

3. Compile:

Git clone https://github.com/mpenkov/ffmpeg-tutorial.gitcd ffmpeg-tutorialmake

4. Run:

/ tutorial01.out ×. Avi above is how to install ffmpeg in the Ubuntu16.04 shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report