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
[code]#include <stdio[b].[/b]h> #include <stdlib[b].[/b]h> #include <iostream> #include <fstream> #include <sstream> extern [color=77FF77]"C"[/color] [color=999999][i]{[/i][/color] [color=999999][i] #include <libavcodec/avcodec.h>[/i][/color] [color=999999][i] #include <libavformat/avformat.h>[/i][/color] [color=999999][i] #include <libavformat/avio.h>[/i][/color] [color=999999][i] #include <libswscale/swscale.h>[/i][/color] [color=999999][i]}[/i][/color] #define log(msg) std::wcout << L[color=77FF77]"[FFmpegRTSP] "[/color] << msg << std::endl << std::flush #define logError(msg)std:: wcout << L[color=77FF77]"[Error] [FFmpegRTSP] "[/color] << msg << std::endl << std::flush AVFormatContext* avFormatContext [b]=[/b] nullptr; SwsContext *swsContext [b]=[/b] nullptr; AVCodecContext* avCodecContext; AVCodec *codec [b]=[/b] nullptr; int frameCount [b]=[/b] [color=FFFF77]0[/color]; void writePPM(AVFrame* frameRGB[b],[/b] int fileSuffix) [color=999999][i]{[/i][/color] [color=999999][i] std::stringstream file_name;[/i][/color] [color=999999][i] std::ofstream output_file;[/i][/color] [color=999999][i] file_name << "test" << fileSuffix << ".ppm";[/i][/color] [color=999999][i] output_file.open(file_name.str().c_str());[/i][/color] [color=999999][i] output_file << "P3 " << avCodecContext->width << " " << avCodecContext->height[/i][/color] [color=999999][i] << " 255\n";[/i][/color] [color=999999][i] for (int y = 0; y < avCodecContext->height; y++) {[/i][/color] [color=999999][i] for (int x = 0; x < avCodecContext->width * 3; x++) {[/i][/color] [color=999999][i] output_file[/i][/color] [color=999999][i] << (int)(frameRGB->data[0][/i][/color] [color=999999][i] + y * frameRGB->linesize[0])[x] << " ";[/i][/color] [color=999999][i] }[/i][/color] } output_file[b].[/b]close(); } int main(int argc[b],[/b] [color=AA77FF]char[/color]** argv) [color=999999][i]{[/i][/color] [color=999999][i] avFormatContext = avformat_alloc_context();[/i][/color] [color=999999][i] if (avformat_open_input(&avFormatContext, "rtsp://admin:123456@192.168.2.109:554/h264Preview_01_main",[/i][/color] [color=999999][i] NULL, NULL) != 0) {[/i][/color] [color=999999][i] logError("Failed to open Stream");[/i][/color] [color=999999][i] return EXIT_FAILURE;[/i][/color] [color=999999][i] }[/i][/color] [color=7777FF][b]if[/b][/color] (avformat_find_stream_info(avFormatContext[b],[/b] NULL) < [color=FFFF77]0[/color]) [color=999999][i]{[/i][/color] [color=999999][i] return EXIT_FAILURE;[/i][/color] [color=999999][i] }[/i][/color] int videoStreamIndex [b]=[/b] [b]-[/b][color=FFFF77]1[/color]; [color=7777FF][b]for[/b][/color] (int i [b]=[/b] [color=FFFF77]0[/color]; i < avFormatContext[b]-[/b]>nb_streams; i[b]+[/b][b]+[/b]) [color=999999][i]{[/i][/color] [color=999999][i] if (avFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {[/i][/color] [color=999999][i] videoStreamIndex = i;[/i][/color] [color=999999][i] }[/i][/color] } [color=7777FF][b]if[/b][/color] (videoStreamIndex [b]=[/b][b]=[/b] [b]-[/b][color=FFFF77]1[/color]) [color=999999][i]{[/i][/color] [color=999999][i] return EXIT_FAILURE;[/i][/color] [color=999999][i] }[/i][/color] av_read_play(avFormatContext); codec [b]=[/b] avcodec_find_decoder(AV_CODEC_ID_H264); [color=7777FF][b]if[/b][/color] ([b]![/b]codec) [color=999999][i]{[/i][/color] [color=999999][i] logError("H264 codec not found, compile FFmpeg with libopenh264 or libx264");[/i][/color] [color=999999][i] return EXIT_FAILURE;[/i][/color] [color=999999][i] }[/i][/color] avCodecContext [b]=[/b] avcodec_alloc_context3(codec); avcodec_get_context_defaults3(avCodecContext[b],[/b] codec); avcodec_copy_context(avCodecContext[b],[/b] avFormatContext[b]-[/b]>streams[b][[/b]videoStreamIndex[b]][/b][b]-[/b]>codec); [color=7777FF][b]if[/b][/color] (avcodec_open2(avCodecContext[b],[/b] codec[b],[/b] NULL) [b]![/b][b]=[/b] [color=FFFF77]0[/color]) [color=999999][i]{[/i][/color] [color=999999][i] logError("Failed to open codec");[/i][/color] [color=999999][i] return EXIT_FAILURE;[/i][/color] [color=999999][i] }[/i][/color] int width [b]=[/b] avCodecContext[b]-[/b]>width; int height [b]=[/b] avCodecContext[b]-[/b]>height; swsContext [b]=[/b] sws_getContext(width[b],[/b] height[b],[/b] avCodecContext[b]-[/b]>pix_fmt[b],[/b] width[b],[/b] height[b],[/b] AV_PIX_FMT_RGB24[b],[/b] SWS_BICUBIC[b],[/b] NULL[b],[/b] NULL[b],[/b] NULL); AVFrame* frame [b]=[/b] av_frame_alloc(); frame[b]-[/b]>format [b]=[/b] avCodecContext[b]-[/b]>pix_fmt; frame[b]-[/b]>width [b]=[/b] width; frame[b]-[/b]>height [b]=[/b] height; av_frame_get_buffer(frame[b],[/b] [color=FFFF77]0[/color]); AVFrame* frameRGB [b]=[/b] av_frame_alloc(); frameRGB[b]-[/b]>format [b]=[/b] AV_PIX_FMT_RGB24; frameRGB[b]-[/b]>width [b]=[/b] width; frameRGB[b]-[/b]>height [b]=[/b] height; av_frame_get_buffer(frameRGB[b],[/b] [color=FFFF77]0[/color]); AVPacket packet; av_init_packet(&packet); [color=7777FF][b]while[/b][/color] (av_read_frame(avFormatContext[b],[/b] &packet) >[b]=[/b] [color=FFFF77]0[/color] && frameCount < [color=FFFF77]1000[/color]) [color=999999][i]{[/i][/color] [color=999999][i] if (packet.stream_index == videoStreamIndex) {[/i][/color] [color=999999][i] int gotFrame = 0;[/i][/color] [color=999999][i] packet.stream_index = frameCount;[/i][/color] [color=999999][i] int result = avcodec_decode_video2(avCodecContext, frame, &gotFrame, &packet);[/i][/color] [color=999999][i] if (frameCount % 100 == 1 && gotFrame){[/i][/color] [color=999999][i] sws_scale(swsContext, frame->data, frame->linesize, 0,[/i][/color] [color=999999][i] avCodecContext->height, frameRGB->data, frameRGB->linesize);[/i][/color] [color=999999][i] writePPM(frameRGB, frameCount);[/i][/color] [color=999999][i] }[/i][/color] frameCount[b]+[/b][b]+[/b]; } 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; } [/code]
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; }