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 [b]/[/b] avcodec.h> #include <libavformat [b]/[/b] avformat.h> #include <libavformat [b]/[/b] avio.h> #include <libswscale [b]/[/b] 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 [b]=[/b] nullptr; SwsContext [b]*[/b] swsContext [b]=[/b] nullptr; AVCodecContext [b]*[/b] avCodecContext; AVCodec [b]*[/b] codec [b]=[/b] nullptr; int frameCount [b]=[/b] [color=FFFF77]0[/color]; void writePPM[b]([/b] AVFrame [b]*[/b] frameRGB[b],[/b] 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 [b]-[/b] >width << [color=77FF77]" "[/color] << avCodecContext [b]-[/b] >height << [color=77FF77]" 255\n"[/color]; [color=7777FF][b]for[/b][/color][b]([/b] int y [b]=[/b] [color=FFFF77]0[/color]; y < avCodecContext [b]-[/b] >height; y [b]+[/b] [b]+[/b] [b])[/b] [b]{[/b] [color=7777FF][b]for[/b][/color][b]([/b] int x [b]=[/b] [color=FFFF77]0[/color]; x < avCodecContext [b]-[/b] >width [b]*[/b] [color=FFFF77]3[/color]; x [b]+[/b] [b]+[/b] [b])[/b] [b]{[/b] output_file <<[b]([/b] int [b])[/b][b]([/b] frameRGB [b]-[/b] >data[b][[/b] [color=FFFF77]0[/color] [b]][/b] [b]+[/b] y [b]*[/b] frameRGB [b]-[/b] >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[b],[/b] char [b]*[/b] [b]*[/b] argv [b])[/b] [b]{[/b] avFormatContext [b]=[/b] avformat_alloc_context[b]([/b][b])[/b] ; [color=7777FF][b]if[/b][/color][b]([/b] avformat_open_input[b]([/b] &avFormatContext[b],[/b] [color=77FF77]"rtsp: // admin:123456@192.168.2.109:554/h264Preview_01_main"[/color][b],[/b] NULL[b],[/b] NULL [b])[/b] [b]![/b][b]=[/b] [color=FFFF77]0[/color] [b])[/b] [b]{[/b] logError[b]([/b] [color=77FF77]"Failed to open Stream"[/color] [b])[/b] ; [color=7777FF][b]return[/b][/color] EXIT_FAILURE; [b]}[/b] [color=7777FF][b]if[/b][/color][b]([/b] avformat_find_stream_info[b]([/b] avFormatContext[b],[/b] NULL [b])[/b] < [color=FFFF77]0[/color] [b])[/b] [b]{[/b] [color=7777FF][b]return[/b][/color] EXIT_FAILURE; [b]}[/b] int videoStreamIndex [b]=[/b] [b]-[/b] [color=FFFF77]1[/color]; [color=7777FF][b]for[/b][/color][b]([/b] int i [b]=[/b] [color=FFFF77]0[/color]; i < avFormatContext [b]-[/b] >nb_streams; i [b]+[/b] [b]+[/b] [b])[/b] [b]{[/b] [color=7777FF][b]if[/b][/color][b]([/b] avFormatContext [b]-[/b] >streams[b][[/b] i [b]][/b] [b]-[/b] >codec [b]-[/b] >codec_type [b]=[/b][b]=[/b] AVMEDIA_TYPE_VIDEO [b])[/b] [b]{[/b] videoStreamIndex [b]=[/b] i; [b]}[/b] [b]}[/b] [color=7777FF][b]if[/b][/color][b]([/b] videoStreamIndex [b]=[/b][b]=[/b] [b]-[/b] [color=FFFF77]1[/color] [b])[/b] [b]{[/b] [color=7777FF][b]return[/b][/color] EXIT_FAILURE; [b]}[/b] av_read_play[b]([/b] avFormatContext [b])[/b] ; codec [b]=[/b] avcodec_find_decoder[b]([/b] AV_CODEC_ID_H264 [b])[/b] ; [color=7777FF][b]if[/b][/color][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] ; [color=7777FF][b]return[/b][/color] EXIT_FAILURE; [b]}[/b] avCodecContext [b]=[/b] avcodec_alloc_context3[b]([/b] codec [b])[/b] ; avcodec_get_context_defaults3[b]([/b] avCodecContext[b],[/b] codec [b])[/b] ; avcodec_copy_context[b]([/b] avCodecContext[b],[/b] avFormatContext [b]-[/b] >streams[b][[/b] videoStreamIndex [b]][/b] [b]-[/b] >codec [b])[/b] ; [color=7777FF][b]if[/b][/color][b]([/b] avcodec_open2[b]([/b] avCodecContext[b],[/b] codec[b],[/b] NULL [b])[/b] [b]![/b][b]=[/b] [color=FFFF77]0[/color] [b])[/b] [b]{[/b] logError[b]([/b] [color=77FF77]"Failed to open codec"[/color] [b])[/b] ; [color=7777FF][b]return[/b][/color] EXIT_FAILURE; [b]}[/b] int width [b]=[/b] avCodecContext [b]-[/b] >width; int height [b]=[/b] avCodecContext [b]-[/b] >height; swsContext [b]=[/b] sws_getContext[b]([/b] 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 [b])[/b] ; AVFrame [b]*[/b] frame [b]=[/b] av_frame_alloc[b]([/b][b])[/b] ; 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[b]([/b] frame[b],[/b] [color=FFFF77]0[/color] [b])[/b] ; AVFrame [b]*[/b] frameRGB [b]=[/b] av_frame_alloc[b]([/b][b])[/b] ; 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[b]([/b] frameRGB[b],[/b] [color=FFFF77]0[/color] [b])[/b] ; AVPacket packet; av_init_packet[b]([/b] &packet [b])[/b] ; [color=7777FF][b]while[/b][/color][b]([/b] av_read_frame[b]([/b] avFormatContext[b],[/b] &packet [b])[/b] > [b]=[/b] [color=FFFF77]0[/color] && frameCount < [color=FFFF77]1000[/color] [b])[/b] [b]{[/b] [color=7777FF][b]if[/b][/color][b]([/b] packet.stream_index [b]=[/b][b]=[/b] videoStreamIndex [b])[/b] [b]{[/b] int gotFrame [b]=[/b] [color=FFFF77]0[/color]; packet.stream_index [b]=[/b] frameCount; int result [b]=[/b] avcodec_decode_video2[b]([/b] avCodecContext[b],[/b] frame[b],[/b] &gotFrame[b],[/b] &packet [b])[/b] ; [color=7777FF][b]if[/b][/color][b]([/b] frameCount % [color=FFFF77]100[/color] [b]=[/b][b]=[/b] [color=FFFF77]1[/color] && gotFrame [b])[/b] [b]{[/b] sws_scale[b]([/b] swsContext[b],[/b] frame [b]-[/b] >data[b],[/b] frame [b]-[/b] >linesize[b],[/b] [color=FFFF77]0[/color][b],[/b] avCodecContext [b]-[/b] >height[b],[/b] frameRGB [b]-[/b] >data[b],[/b] frameRGB [b]-[/b] >linesize [b])[/b] ; writePPM[b]([/b] frameRGB[b],[/b] frameCount [b])[/b] ; [b]}[/b] frameCount [b]+[/b] [b]+[/b] ; [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] ; [color=7777FF][b]return[/b][/color] EXIT_SUCCESS; [b]}[/b] [/code]
Diesen Code in Original-Formatierung anzeigen
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; }