热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

ffmpeg远程监控桌面

***ref.雷霄骅LeiXiaohua*#include#include#include#pragmacom

/*** ref. 雷霄骅 Lei Xiaohua*/
#include
#include
#include
#pragma comment(lib,"ws2_32.lib")
#define __STDC_CONSTANT_MACROS
extern "C"
{
#include "include/libavcodec/avcodec.h"
#include "include/libavformat/avformat.h"
#include "include/libswscale/swscale.h"
#include "include/libavdevice/avdevice.h"
#include "include/libavutil/imgutils.h"
#include "include/libavutil/opt.h"
#include "include/libavutil/imgutils.h"
//#include "includes/SDL.h"
};int Send(uint8_t * buf ,int size) {return 0;
}
struct sockaddr_in G_Servaddr;int main(int argc, char* argv[])
{AVFormatContext *pFormatContext &#61; NULL;int i &#61; 0, videoindex;AVCodecContext *pCodecContext &#61; NULL;AVCodec *pCodec &#61; NULL;FILE *fp_out;fp_out &#61; fopen("ds.h264", "wb");AVPacket pPacket; int y_size;FILE *fp_yuv &#61; fopen("output.yuv", "wb&#43;");struct SwsContext *img_convert_ctx;AVFormatContext *pFormatContextEncod &#61; NULL;AVCodecContext *pCodecContextEncod &#61; NULL;AVCodec *pCodecEncod &#61; NULL;AVCodecID codec_id &#61; AV_CODEC_ID_H264;int in_w &#61; 1920, in_h &#61; 1080;int framenum &#61; 100;int ret, got_output, xy_size, got_picture;/*socket--------------------------------*/WSADATA wsa; if (WSAStartup(MAKEWORD(2, 2), &wsa) !&#61; 0){printf("WSAStartup failed!\n");return 1;}int connfd;//socklen_t addrlen(0);SOCKET ServerS &#61; socket(AF_INET, SOCK_DGRAM, 0);int WSAStartup(WORD wVersionRequested,LPWSADATA lpWSAData);SOCKADDR_IN DistAddr;DistAddr.sin_family &#61; AF_INET;DistAddr.sin_port &#61; htons(8800);DistAddr.sin_addr.s_addr &#61; inet_addr("127.0.0.1"); //inet_addr("192.168.23.232");if (DistAddr.sin_addr.s_addr &#61;&#61; INADDR_NONE){printf("不可用地址!\n");return -1;}int time_out &#61; 2000;//ret &#61; setsockopt(ServerS, SOL_SOCKET, SO_RCVTIMEO, (char*)&time_out, sizeof(time_out));char buf[150000]; //1500 bytes/*socket--------------------------------*/av_register_all();avformat_network_init();pFormatContext &#61; avformat_alloc_context();avdevice_register_all();AVDictionary* options &#61; NULL;av_dict_set(&options, "video_size", "1920*1080", 0);//设定捕捉范围av_dict_set(&options, "framerate", "25", 0);AVInputFormat *ifmt &#61; av_find_input_format("gdigrab");if (avformat_open_input(&pFormatContext, "desktop", ifmt, &options) !&#61; 0){ printf("Couldn&#39;t open input stream.\n"); return -1; }if (avformat_find_stream_info(pFormatContext, NULL) <0)//找到流{ printf("Couldn&#39;t find stream information.\n"); return -1; }videoindex &#61; -1;for (i &#61; 0; i nb_streams; i&#43;&#43;){if (pFormatContext->streams[i]->codec->codec_type &#61;&#61; AVMEDIA_TYPE_VIDEO)//流的数目计数{videoindex &#61; i; break;}}if (videoindex &#61;&#61; -1){ printf("Didn&#39;t find a video stream.\n"); return -1; }av_dump_format(pFormatContext, 0, 0, 0);pCodecContext &#61; pFormatContext->streams[videoindex]->codec;//编码器参数直接取数据流的参数&#xff08;桌面流pCodec &#61; avcodec_find_decoder(pCodecContext->codec_id);//解码器 BMP解码if (pCodec &#61;&#61; NULL){printf("Codec not found.\n");return -1;}if (avcodec_open2(pCodecContext, pCodec, NULL) <0)//在环境中打开这个编码器{printf("Could not open codec.\n");return -1;}AVFrame *pFrame, *pFrameYUV;pFrame &#61; av_frame_alloc();pFrameYUV &#61; av_frame_alloc();uint8_t *out_buffer;AVPacket *packet;out_buffer &#61; (unsigned char *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P, pCodecContext->width, pCodecContext->height, 1));av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize, out_buffer, AV_PIX_FMT_YUV420P, pCodecContext->width, pCodecContext->height, 1);packet &#61; (AVPacket *)av_malloc(sizeof(AVPacket));printf("--------------- File Information ----------------\n");//手工调试函数&#xff0c;输出tbn、tbc、tbr、PAR、DAR的含义av_dump_format(pFormatContext, 0, 0, 0);初始化输出流上下文。//AVFormatContext * output_format_context_ &#61; NULL;//avformat_alloc_output_context2(&output_format_context_, NULL, "rtsp", "rtsp://127.0.0.1:8800/");//AVDictionary *format_opts &#61; NULL;//av_dict_set(&format_opts, "stimeout", "2000000", 0);//av_dict_set(&format_opts, "rtsp_transport", "tcp", 0);//avformat_write_header(output_format_context_, &format_opts);// encode-----------------------------------pCodecEncod &#61; avcodec_find_encoder(codec_id);if (!pCodecEncod) {printf("Codec not found\n");return -1;}pCodecContextEncod &#61; avcodec_alloc_context3(pCodecEncod);if (!pCodecContextEncod) {printf("Could not allocate video codec context\n");return -1;}pCodecContextEncod->bit_rate &#61; 1000000;/*比特率越高&#xff0c;传送的数据越大,越清晰*/pCodecContextEncod->width &#61; in_w;pCodecContextEncod->height &#61; in_h;pCodecContextEncod->time_base.num &#61; 1;pCodecContextEncod->time_base.den &#61; 25;pCodecContextEncod->gop_size &#61; 50;pCodecContextEncod->max_b_frames &#61; 1;pCodecContextEncod->pix_fmt &#61; AV_PIX_FMT_YUV420P;/* 关键帧的周期&#xff0c;也就是两个IDR帧之间的距离&#xff0c;一个帧组的最大帧数&#xff0c;一般而言&#xff0c;每一秒视频至少需要使用 1 个关键帧。增加关键帧个数可改善质量&#xff0c;但是同时增加带宽和网络负载。*/av_opt_set(pCodecContextEncod->priv_data, "preset", "slow", 0);if (avcodec_open2(pCodecContextEncod, pCodecEncod, NULL) <0) {printf("Could not open codec\n");return -1;}xy_size &#61; pCodecContextEncod->width * pCodecContextEncod->height;// encode-----------------------------------//&packet &#61; (AVPacket *)av_malloc(sizeof(AVPacket));img_convert_ctx &#61; sws_getContext(pCodecContext->width, pCodecContext->height, pCodecContext->pix_fmt,pCodecContext->width, pCodecContext->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);i &#61; 0;//读取数据while (av_read_frame(pFormatContext, packet) >&#61; 0 && i <200){if (packet->stream_index &#61;&#61; videoindex){av_init_packet(&pPacket);pPacket.data &#61; NULL;pPacket.size &#61; 0;pFrame->pts &#61; i;ret &#61; avcodec_decode_video2(pCodecContext, pFrame, &got_picture, packet);if (ret <0){printf("Decode Error.\n");return -1;}if (got_picture >&#61; 1){//成功解码一帧sws_scale(img_convert_ctx, (const unsigned char* const*)pFrame->data, pFrame->linesize, 0, pCodecContext->height,pFrameYUV->data, pFrameYUV->linesize);//转换图像格式y_size &#61; pCodecContext->width*pCodecContext->height;fwrite(pFrameYUV->data[0], 1, y_size, fp_yuv); //Y fwrite(pFrameYUV->data[1], 1, y_size / 4, fp_yuv); //U Cb fwrite(pFrameYUV->data[2], 1, y_size / 4, fp_yuv); //V Cr //printf("Succeed to decode-scale-fwrite 1 frame!\n");pFrameYUV->width &#61; 1920;pFrameYUV->height &#61; 1080;pFrameYUV->format &#61; AV_PIX_FMT_YUV420P;/*encode-----------------------*/ret &#61; avcodec_encode_video2(pCodecContextEncod, &pPacket, pFrameYUV, &got_output);if (ret <0) {printf("Error encoding frame\n");return -1;}if (got_output) {// printf("Succeed to encode frame: %5d\tsize:%5d\n", framenum, pPacket.size);framenum&#43;&#43;;fwrite(pPacket.data, 1, pPacket.size, fp_out);/*sendudp----------------------*/memset(buf,0, sizeof(buf));//int a;memcpy(buf, &pPacket.data, pPacket.size);int result &#61; sendto(ServerS, buf, sizeof(pPacket.size)&#43;1, 0, (SOCKADDR *)&DistAddr, sizeof(DistAddr));
// int ret1 &#61; av_interleaved_write_frame(output_format_context_, &pPacket);av_free_packet(&pPacket);}/*sendudp----------------------*//*encode-----------------------*/pFrameYUV->pts&#43;&#43;;i&#43;&#43;;}else{//未解码到一帧&#xff0c;可能时结尾B帧或延迟帧&#xff0c;在后面做flush decoder处理}}av_free_packet(packet);}av_dump_format(pFormatContext, 0, 0, 0);//Flush Decoderwhile (true){if (!(pCodec->capabilities & AV_CODEC_CAP_DELAY))break;ret &#61; avcodec_decode_video2(pCodecContext, pFrame, &got_picture, NULL);if (ret <0){break;}if (!got_picture){break;}sws_scale(img_convert_ctx, (const unsigned char* const*)pFrame->data, pFrame->linesize, 0, pCodecContext->height,pFrameYUV->data, pFrameYUV->linesize);y_size &#61; pCodecContext->width*pCodecContext->height;fwrite(pFrameYUV->data[0], 1, y_size, fp_yuv); //Y fwrite(pFrameYUV->data[1], 1, y_size / 4, fp_yuv); //Ufwrite(pFrameYUV->data[2], 1, y_size / 4, fp_yuv); //Vprintf("Flush Decoder: Succeed to decode 1 frame!\n");}//Flush Encoderfor (got_output &#61; 1; got_output; i&#43;&#43;) {ret &#61; avcodec_encode_video2(pCodecContextEncod, &pPacket, NULL, &got_output);if (ret <0) {printf("Error encoding frame\n");return -1;}if (got_output) {printf("Flush Encoder: Succeed to encode 1 frame!\tsize:%5d\n", pPacket.size);fwrite(pPacket.data, 1, pPacket.size, fp_out);//av_write_frame(pFormatCtx, &pkt);av_free_packet(&pPacket);}}closesocket(ServerS);WSACleanup();sws_freeContext(img_convert_ctx);av_frame_free(&pFrameYUV);av_frame_free(&pFrame);avcodec_close(pCodecContext);avformat_close_input(&pFormatContext);fclose(fp_yuv);fclose(fp_out);avcodec_close(pCodecContextEncod);av_free(pCodecEncod);//av_freep(&pFrame->data[0]);//av_frame_free(&pFrame);//std:cin>>i>>endl;scanf("%d",&i);return 0;}


推荐阅读
  • 在1995年,Simon Plouffe 发现了一种特殊的求和方法来表示某些常数。两年后,Bailey 和 Borwein 在他们的论文中发表了这一发现,这种方法被命名为 Bailey-Borwein-Plouffe (BBP) 公式。该问题要求计算圆周率 π 的第 n 个十六进制数字。 ... [详细]
  • 二维码的实现与应用
    本文介绍了二维码的基本概念、分类及其优缺点,并详细描述了如何使用Java编程语言结合第三方库(如ZXing和qrcode.jar)来实现二维码的生成与解析。 ... [详细]
  • 本文通过C++语言实现了一个递归算法,用于解析并计算数学表达式的值。该算法能够处理加法、减法、乘法和除法操作。 ... [详细]
  • 问题描述现在,不管开发一个多大的系统(至少我现在的部门是这样的),都会带一个日志功能;在实际开发过程中 ... [详细]
  • Web动态服务器Python基本实现
    Web动态服务器Python基本实现 ... [详细]
  • Android与JUnit集成测试实践
    本文探讨了如何在Android项目中集成JUnit进行单元测试,并详细介绍了修改AndroidManifest.xml文件以支持测试的方法。 ... [详细]
  • linux网络子系统分析(二)—— 协议栈分层框架的建立
    目录一、综述二、INET的初始化2.1INET接口注册2.2抽象实体的建立2.3代码细节分析2.3.1socket参数三、其他协议3.1PF_PACKET3.2P ... [详细]
  • HTML:  将文件拖拽到此区域 ... [详细]
  • Logging all MySQL queries into the Slow Log
    MySQLoptionallylogsslowqueriesintotheSlowQueryLog–orjustSlowLog,asfriendscallit.However,Thereareseveralreasonstologallqueries.Thislistisnotexhaustive:Belowyoucanfindthevariablestochange,astheyshouldbewritteninth ... [详细]
  • c语言二元插值,二维线性插值c语言
    c语言二元插值,二维线性插值c语言 ... [详细]
  • 在测试软件或进行系统维护时,有时会遇到电脑蓝屏的情况,即便使用了沙盒环境也无法完全避免。本文将详细介绍常见的蓝屏错误代码及其解决方案,帮助用户快速定位并解决问题。 ... [详细]
  • 本文详细介绍了笔记本电脑上多种实用的快捷键,包括屏幕调整、图形设置、分辨率更改、驱动更新、导航操作、音量控制及屏幕锁定等,旨在帮助用户更高效地使用笔记本电脑。 ... [详细]
  • 本题要求计算一组正整数的最小公倍数(LCM)。输入包括多组测试数据,每组数据首先给出一个正整数n,随后是n个正整数。 ... [详细]
  • 使用QT构建基础串口辅助工具
    本文详细介绍了如何利用QT框架创建一个简易的串口助手应用程序,包括项目的建立、界面设计与编程实现、运行测试以及最终的应用程序打包。 ... [详细]
  • 线段树详解与实现
    本文详细介绍了线段树的基本概念及其在编程竞赛中的应用,并提供了一个具体的线段树实现代码示例。 ... [详细]
author-avatar
哦是你的嘛_416
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有