Skip to content
Home
Post Code
Jass (Warcraft III)
Wurst
GUI (Warcraft III)
Galaxy (StarCraft II)
GUI (StarCraft II)
C++
Java
Delphi
Text
Search Code
Compare Code
Uploads
Draw
Create New Drawing
View Drawings
Graph
Log in
<pre class="jasscode">#include <stdio<span class="style_symbol">.</span>h> #include <stdlib<span class="style_symbol">.</span>h> #include <iostream> #include <fstream> #include <sstream> extern <span class="style_string">"C"</span> <span class="style_comment">{</span> <span class="style_comment"> #include <libavcodec/avcodec.h></span> <span class="style_comment"> #include <libavformat/avformat.h></span> <span class="style_comment"> #include <libavformat/avio.h></span> <span class="style_comment"> #include <libswscale/swscale.h></span> <span class="style_comment">}</span> #define log(msg) std::wcout << L<span class="style_string">"[FFmpegRTSP] "</span> << msg << std::endl << std::flush #define logError(msg)std:: wcout << L<span class="style_string">"[Error] [FFmpegRTSP] "</span> << msg << std::endl << std::flush AVFormatContext* avFormatContext <span class="style_symbol">=</span> nullptr; SwsContext *swsContext <span class="style_symbol">=</span> nullptr; AVCodecContext* avCodecContext; AVCodec *codec <span class="style_symbol">=</span> nullptr; int frameCount <span class="style_symbol">=</span> <span class="style_int">0</span>; void writePPM(AVFrame* frameRGB<span class="style_symbol">,</span> int fileSuffix) <span class="style_comment">{</span> <span class="style_comment"> std::stringstream file_name;</span> <span class="style_comment"> std::ofstream output_file;</span> <span class="style_comment"> file_name << "test" << fileSuffix << ".ppm";</span> <span class="style_comment"> output_file.open(file_name.str().c_str());</span> <span class="style_comment"> output_file << "P3 " << avCodecContext->width << " " << avCodecContext->height</span> <span class="style_comment"> << " 255\n";</span> <span class="style_comment"> for (int y = 0; y < avCodecContext->height; y++) {</span> <span class="style_comment"> for (int x = 0; x < avCodecContext->width * 3; x++) {</span> <span class="style_comment"> output_file</span> <span class="style_comment"> << (int)(frameRGB->data[0]</span> <span class="style_comment"> + y * frameRGB->linesize[0])[x] << " ";</span> <span class="style_comment"> }</span> } output_file<span class="style_symbol">.</span>close(); } int main(int argc<span class="style_symbol">,</span> <span class="style_type">char</span>** argv) <span class="style_comment">{</span> <span class="style_comment"> avFormatContext = avformat_alloc_context();</span> <span class="style_comment"> if (avformat_open_input(&avFormatContext, "rtsp://admin:123456@192.168.2.109:554/h264Preview_01_main",</span> <span class="style_comment"> NULL, NULL) != 0) {</span> <span class="style_comment"> logError("Failed to open Stream");</span> <span class="style_comment"> return EXIT_FAILURE;</span> <span class="style_comment"> }</span> <span class="style_keyword">if</span> (avformat_find_stream_info(avFormatContext<span class="style_symbol">,</span> NULL) < <span class="style_int">0</span>) <span class="style_comment">{</span> <span class="style_comment"> return EXIT_FAILURE;</span> <span class="style_comment"> }</span> int videoStreamIndex <span class="style_symbol">=</span> <span class="style_symbol">-</span><span class="style_int">1</span>; <span class="style_keyword">for</span> (int i <span class="style_symbol">=</span> <span class="style_int">0</span>; i < avFormatContext<span class="style_symbol">-</span>>nb_streams; i<span class="style_symbol">+</span><span class="style_symbol">+</span>) <span class="style_comment">{</span> <span class="style_comment"> if (avFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {</span> <span class="style_comment"> videoStreamIndex = i;</span> <span class="style_comment"> }</span> } <span class="style_keyword">if</span> (videoStreamIndex <span class="style_symbol">=</span><span class="style_symbol">=</span> <span class="style_symbol">-</span><span class="style_int">1</span>) <span class="style_comment">{</span> <span class="style_comment"> return EXIT_FAILURE;</span> <span class="style_comment"> }</span> av_read_play(avFormatContext); codec <span class="style_symbol">=</span> avcodec_find_decoder(AV_CODEC_ID_H264); <span class="style_keyword">if</span> (<span class="style_symbol">!</span>codec) <span class="style_comment">{</span> <span class="style_comment"> logError("H264 codec not found, compile FFmpeg with libopenh264 or libx264");</span> <span class="style_comment"> return EXIT_FAILURE;</span> <span class="style_comment"> }</span> avCodecContext <span class="style_symbol">=</span> avcodec_alloc_context3(codec); avcodec_get_context_defaults3(avCodecContext<span class="style_symbol">,</span> codec); avcodec_copy_context(avCodecContext<span class="style_symbol">,</span> avFormatContext<span class="style_symbol">-</span>>streams<span class="style_symbol">[</span>videoStreamIndex<span class="style_symbol">]</span><span class="style_symbol">-</span>>codec); <span class="style_keyword">if</span> (avcodec_open2(avCodecContext<span class="style_symbol">,</span> codec<span class="style_symbol">,</span> NULL) <span class="style_symbol">!</span><span class="style_symbol">=</span> <span class="style_int">0</span>) <span class="style_comment">{</span> <span class="style_comment"> logError("Failed to open codec");</span> <span class="style_comment"> return EXIT_FAILURE;</span> <span class="style_comment"> }</span> int width <span class="style_symbol">=</span> avCodecContext<span class="style_symbol">-</span>>width; int height <span class="style_symbol">=</span> avCodecContext<span class="style_symbol">-</span>>height; swsContext <span class="style_symbol">=</span> sws_getContext(width<span class="style_symbol">,</span> height<span class="style_symbol">,</span> avCodecContext<span class="style_symbol">-</span>>pix_fmt<span class="style_symbol">,</span> width<span class="style_symbol">,</span> height<span class="style_symbol">,</span> AV_PIX_FMT_RGB24<span class="style_symbol">,</span> SWS_BICUBIC<span class="style_symbol">,</span> NULL<span class="style_symbol">,</span> NULL<span class="style_symbol">,</span> NULL); AVFrame* frame <span class="style_symbol">=</span> av_frame_alloc(); frame<span class="style_symbol">-</span>>format <span class="style_symbol">=</span> avCodecContext<span class="style_symbol">-</span>>pix_fmt; frame<span class="style_symbol">-</span>>width <span class="style_symbol">=</span> width; frame<span class="style_symbol">-</span>>height <span class="style_symbol">=</span> height; av_frame_get_buffer(frame<span class="style_symbol">,</span> <span class="style_int">0</span>); AVFrame* frameRGB <span class="style_symbol">=</span> av_frame_alloc(); frameRGB<span class="style_symbol">-</span>>format <span class="style_symbol">=</span> AV_PIX_FMT_RGB24; frameRGB<span class="style_symbol">-</span>>width <span class="style_symbol">=</span> width; frameRGB<span class="style_symbol">-</span>>height <span class="style_symbol">=</span> height; av_frame_get_buffer(frameRGB<span class="style_symbol">,</span> <span class="style_int">0</span>); AVPacket packet; av_init_packet(&packet); <span class="style_keyword">while</span> (av_read_frame(avFormatContext<span class="style_symbol">,</span> &packet) ><span class="style_symbol">=</span> <span class="style_int">0</span> && frameCount < <span class="style_int">1000</span>) <span class="style_comment">{</span> <span class="style_comment"> if (packet.stream_index == videoStreamIndex) {</span> <span class="style_comment"> int gotFrame = 0;</span> <span class="style_comment"> packet.stream_index = frameCount;</span> <span class="style_comment"> int result = avcodec_decode_video2(avCodecContext, frame, &gotFrame, &packet);</span> <span class="style_comment"> if (frameCount % 100 == 1 && gotFrame){</span> <span class="style_comment"> sws_scale(swsContext, frame->data, frame->linesize, 0,</span> <span class="style_comment"> avCodecContext->height, frameRGB->data, frameRGB->linesize);</span> <span class="style_comment"> writePPM(frameRGB, frameCount);</span> <span class="style_comment"> }</span> frameCount<span class="style_symbol">+</span><span class="style_symbol">+</span>; } av_free_packet(&packet); av_init_packet(&packet); } av_frame_free(&frame); av_frame_free(&frameRGB); av_read_pause(avFormatContext); avcodec_free_context(&avCodecContext); avformat_free_context(avFormatContext); return EXIT_SUCCESS; } </pre>
select language
jass
java
> delphi
C++
php
GSL
galaxy
visualBasic
wurst
Text
Markdown
select style
default
bbcode - default
Jasscraft
> jasscraft html
visual
lightweight
lightweightNr
goto line:
Compare with:
text copy window
edit this code
post new code
#include <stdio.h> #include <stdlib.h> #include <iostream> #include <fstream> #include <sstream> extern "C" { #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libavformat/avio.h> #include <libswscale/swscale.h> } #define log(msg) std::wcout << L"[FFmpegRTSP] " << msg << std::endl << std::flush #define logError(msg)std:: wcout << L"[Error] [FFmpegRTSP] " << msg << std::endl << std::flush AVFormatContext* avFormatContext = nullptr; SwsContext *swsContext = nullptr; AVCodecContext* avCodecContext; AVCodec *codec = nullptr; int frameCount = 0; void writePPM(AVFrame* frameRGB, int fileSuffix) { std::stringstream file_name; std::ofstream output_file; file_name << "test" << fileSuffix << ".ppm"; output_file.open(file_name.str().c_str()); output_file << "P3 " << avCodecContext->width << " " << avCodecContext->height << " 255\n"; for (int y = 0; y < avCodecContext->height; y++) { for (int x = 0; x < avCodecContext->width * 3; x++) { output_file << (int)(frameRGB->data[0] + y * frameRGB->linesize[0])[x] << " "; } } output_file.close(); } int main(int argc, char** argv) { avFormatContext = avformat_alloc_context(); if (avformat_open_input(&avFormatContext, "rtsp://admin:123456@192.168.2.109:554/h264Preview_01_main", NULL, NULL) != 0) { logError("Failed to open Stream"); return EXIT_FAILURE; } if (avformat_find_stream_info(avFormatContext, NULL) < 0) { return EXIT_FAILURE; } int videoStreamIndex = -1; for (int i = 0; i < avFormatContext->nb_streams; i++) { if (avFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) { videoStreamIndex = i; } } if (videoStreamIndex == -1) { return EXIT_FAILURE; } av_read_play(avFormatContext); codec = avcodec_find_decoder(AV_CODEC_ID_H264); if (!codec) { logError("H264 codec not found, compile FFmpeg with libopenh264 or libx264"); return EXIT_FAILURE; } avCodecContext = avcodec_alloc_context3(codec); avcodec_get_context_defaults3(avCodecContext, codec); avcodec_copy_context(avCodecContext, avFormatContext->streams[videoStreamIndex]->codec); if (avcodec_open2(avCodecContext, codec, NULL) != 0) { logError("Failed to open codec"); return EXIT_FAILURE; } int width = avCodecContext->width; int height = avCodecContext->height; swsContext = sws_getContext(width, height, avCodecContext->pix_fmt, width, height, AV_PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL); AVFrame* frame = av_frame_alloc(); frame->format = avCodecContext->pix_fmt; frame->width = width; frame->height = height; av_frame_get_buffer(frame, 0); AVFrame* frameRGB = av_frame_alloc(); frameRGB->format = AV_PIX_FMT_RGB24; frameRGB->width = width; frameRGB->height = height; av_frame_get_buffer(frameRGB, 0); AVPacket packet; av_init_packet(&packet); while (av_read_frame(avFormatContext, &packet) >= 0 && frameCount < 1000) { if (packet.stream_index == videoStreamIndex) { int gotFrame = 0; packet.stream_index = frameCount; int result = avcodec_decode_video2(avCodecContext, frame, &gotFrame, &packet); if (frameCount % 100 == 1 && gotFrame){ sws_scale(swsContext, frame->data, frame->linesize, 0, avCodecContext->height, frameRGB->data, frameRGB->linesize); writePPM(frameRGB, frameCount); } frameCount++; } av_free_packet(&packet); av_init_packet(&packet); } av_frame_free(&frame); av_frame_free(&frameRGB); av_read_pause(avFormatContext); avcodec_free_context(&avCodecContext); avformat_free_context(avFormatContext); return EXIT_SUCCESS; }