The simplest FFMPEG-based streamer attachment: streamer and ffmpeg attachment

Source: Internet
Author: User
Tags flv file

The simplest FFMPEG-based streamer attachment: streamer and ffmpeg attachment

========================================================== ==================

List of the simplest FFmpeg-based streamer series:

The simplest FFmpeg-based streamer (using RTMP push as an example)

The simplest FFMPEG-based streamer attachment: streamer

========================================================== ==================


To supplement the simplest FFmpeg-based streamer, this article records a simplest FFmpeg-based streamer. The streamer and streamer play the opposite role: the streamer is used to send local files as streaming media, while the streamer is used to save streaming media content as local files.

The streamer recorded in this article can save RTMP streaming media as a local FLV file. Because FFmpeg supports many streaming media protocols and encapsulation formats, it also supports other encapsulation formats and streaming media protocols.

Source code

/*** The Simplest FFmpeg-based Receiver (receives RTMP) * Simplest FFmpeg Receiver Er (Receive RTMP) ** leixiao Lei Xiaohua * leixiaohua1020@126.com * China Media University/Digital TV Technology * Communication University of China/Digital TV Technology * http://blog.csdn.net/leixiaohua1020 ** this example uses streaming media data (RTMP as an example) save the local files. * It is the simplest tutorial to use FFmpeg to receive streaming media. ** This example saves streaming media data (Use RTMP as example) * as a local file. * It's the simplest FFmpeg stream aggreger. **/# include <stdio. h> # define _ STDC_CONSTANT_MACROS # ifdef _ WIN32 // Windowsextern "C" {# include "libavformat/avformat. h "# include" libavutil/mathematics. h "# include" libavutil/time. h "}; # else // Linux... # ifdef _ cplusplusextern "C" {# endif # include <libavformat/avformat. h> # includ E <libavutil/mathematics. h> # include <libavutil/time. h> # ifdef _ cplusplus}; # endif // '1': Use H. 264 Bitstream Filter # define use_hsf-bsf 0int main (int argc, char * argv []) {AVOutputFormat * ofmt = NULL; // Input AVFormatContext and Output AVFormatContextAVFormatContext * ifmt_ctx = NULL, * ofmt_ctx = NULL; AVPacket pkt; const char * in_filename, * out_filename; int ret, I; int videoindex =-1; int frame_index = 0; in_filename = "rtmp: // live.hkstv.hk.lxdns.com/live/hks"; // in_filename = "rtp: // 233.233.233.233: 6666"; // out_filename = "receive. ts "; // out_filename =" receive.mkv "; out_filename =" receive. flv "; av_register_all (); // Networkavformat_network_init (); // Inputif (ret = avformat_open_input (& ifmt_ctx, in_filename, 0, 0) <0) {printf ("cocould not open input file. "); goto end;} if (ret = avformat_find_stream_ I Nfo (ifmt_ctx, 0) <0) {printf ("Failed to retrieve input stream information"); goto end ;}for (I = 0; I <ifmt_ctx-> nb_streams; I ++) if (ifmt_ctx-> streams [I]-> codec-> codec_type = AVMEDIA_TYPE_VIDEO) {videoindex = I; break;} av_dump_format (ifmt_ctx, 0, in_filename, 0); // Outputavformat_alloc_output_context2 (& ofmt_ctx, NULL, NULL, out_filename); // RTMPif (! Ofmt_ctx) {printf ("cocould not create output context \ n"); ret = AVERROR_UNKNOWN; goto end;} ofmt = ofmt_ctx-> oformat; for (I = 0; I <ifmt_ctx-> nb_streams; I ++) {// Create output AVStream according to input AVStreamAVStream * in_stream = ifmt_ctx-> streams [I]; AVStream * out_stream = avformat_new_stream (mtof_ctx, in_stream-> codec); if (! Out_stream) {printf ("Failed allocating output stream \ n"); ret = AVERROR_UNKNOWN; goto end;} // Copy the settings of AVCodecContextret = avcodec_copy_context (out_stream-> codec, in_stream-> codec); if (ret <0) {printf ("Failed to copy context from input to output stream codec context \ n"); goto end ;} out_stream-> codec-> codec_tag = 0; if (ofmt_ctx-> oformat-> flags & AVFMT_GLOBALHEADER) out_stream-> codec-> flags | = CODEC_FLAG_GLOBAL_HEADER;} // Dump Format ------------------ av_dump_format (ofmt_ctx, 0, out_filename, 1); // Open output URLif (! (Ofmt-> flags & AVFMT_NOFILE) {ret = avio_open (& ofmt_ctx-> pb, out_filename, AVIO_FLAG_WRITE); if (ret <0) {printf ("cocould not open output URL '% S'", out_filename); goto end ;}// Write file headerret = avformat_write_header (ofmt_ctx, NULL ); if (ret <0) {printf ("Error occurred when opening output URL \ n"); goto end ;}# if use_hsf-bsfavbitstreamfiltercontext * hsf-bsfc = av_bitstream_filter_init ("hsf-_mp4to Listen B "); # endifwhile (1) {AVStream * in_stream, * out_stream; // Get an AVPacketret = av_read_frame (ifmt_ctx, & pkt); if (ret <0) break; in_stream = ifmt_ctx-> streams [pkt. stream_index]; out_stream = ofmt_ctx-> streams [pkt. stream_index];/* copy packet * // Convert PTS/DTSpkt. pts = av_rescale_q_rnd (pkt. pts, in_stream-> time_base, out_stream-> time_base, (AVRounding) (AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX); pkt. Dts = av_rescale_q_rnd (pkt. dts, in_stream-> time_base, out_stream-> time_base, (AVRounding) (AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX); pkt. duration = av_rescale_q (pkt. duration, in_stream-> time_base, out_stream-> time_base); pkt. pos =-1; // Print to Screenif (pkt. stream_index = videoindex) {printf ("Receive % 8d video frames from input URL \ n", frame_index); frame_index ++; # if use_hsf-bsfav_bitstream_filter_filte R (hsf-bsfc, in_stream-> codec, NULL, & pkt. data, & pkt. size, pkt. data, pkt. size, 0); # endif} // ret = av_write_frame (ofmt_ctx, & pkt); ret = av_interleaved_write_frame (ofmt_ctx, & pkt); if (ret <0) {printf ("Error muxing packet \ n"); break;} av_free_packet (& pkt) ;}# if transform (hsf-bsfc); # endif // Write file trailerav_write_trailer (ofmt_ctx ); end: avformat_close_input (& ifmt_ctx);/* clos E output */if (ofmt_ctx &&! (Ofmt-> flags & AVFMT_NOFILE) avio_close (ofmt_ctx-> pb); avformat_free_context (ofmt_ctx); if (ret <0 & ret! = AVERROR_EOF) {printf ("Error occurred. \ n"); return-1;} return 0 ;}

After running the result program, you can obtain streaming media data and save it as a video file locally.

Download


Simplest ffmpeg streamer


Project Homepage

SourceForge: https://sourceforge.net/projects/simplestffmpegstreamer/

Github: https://github.com/leixiaohua1020/simplest_ffmpeg_streamer

Open source China: http://git.oschina.net/leixiaohua1020/simplest_ffmpeg_streamer



The solution contains two projects:

Simplest_ffmpeg_streamer: pushes local video files to the Streaming Media Server.

Simplest_ffmpeg_receiver: saves streaming media data to local files.


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.