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