热门标签 | 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;}


推荐阅读
  • c语言\n不换行,c语言printf不换行
    本文目录一览:1、C语言不换行输入2、c语言的 ... [详细]
  • 本文介绍了为什么要使用多进程处理TCP服务端,多进程的好处包括可靠性高和处理大量数据时速度快。然而,多进程不能共享进程空间,因此有一些变量不能共享。文章还提供了使用多进程实现TCP服务端的代码,并对代码进行了详细注释。 ... [详细]
  • 本文介绍了解决二叉树层序创建问题的方法。通过使用队列结构体和二叉树结构体,实现了入队和出队操作,并提供了判断队列是否为空的函数。详细介绍了解决该问题的步骤和流程。 ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • 本文介绍了一种划分和计数油田地块的方法。根据给定的条件,通过遍历和DFS算法,将符合条件的地块标记为不符合条件的地块,并进行计数。同时,还介绍了如何判断点是否在给定范围内的方法。 ... [详细]
  • 本文介绍了C函数ispunct()的用法及示例代码。ispunct()函数用于检查传递的字符是否是标点符号,如果是标点符号则返回非零值,否则返回零。示例代码演示了如何使用ispunct()函数来判断字符是否为标点符号。 ... [详细]
  • 动态规划算法的基本步骤及最长递增子序列问题详解
    本文详细介绍了动态规划算法的基本步骤,包括划分阶段、选择状态、决策和状态转移方程,并以最长递增子序列问题为例进行了详细解析。动态规划算法的有效性依赖于问题本身所具有的最优子结构性质和子问题重叠性质。通过将子问题的解保存在一个表中,在以后尽可能多地利用这些子问题的解,从而提高算法的效率。 ... [详细]
  • Linux环境变量函数getenv、putenv、setenv和unsetenv详解
    本文详细解释了Linux中的环境变量函数getenv、putenv、setenv和unsetenv的用法和功能。通过使用这些函数,可以获取、设置和删除环境变量的值。同时给出了相应的函数原型、参数说明和返回值。通过示例代码演示了如何使用getenv函数获取环境变量的值,并打印出来。 ... [详细]
  • 本文介绍了PE文件结构中的导出表的解析方法,包括获取区段头表、遍历查找所在的区段等步骤。通过该方法可以准确地解析PE文件中的导出表信息。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 本文介绍了一个题目的解法,通过二分答案来解决问题,但困难在于如何进行检查。文章提供了一种逃逸方式,通过移动最慢的宿管来锁门时跑到更居中的位置,从而使所有合格的寝室都居中。文章还提到可以分开判断两边的情况,并使用前缀和的方式来求出在任意时刻能够到达宿管即将锁门的寝室的人数。最后,文章提到可以改成O(n)的直接枚举来解决问题。 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • 本文讨论了clone的fork与pthread_create创建线程的不同之处。进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合。在调用系统调用fork创建一个进程时,子进程只是完全复制父进程的资源,这样得到的子进程独立于父进程,具有良好的并发性。但是二者之间的通讯需要通过专门的通讯机制,另外通过fork创建子进程系统开销很大。因此,在某些情况下,使用clone或pthread_create创建线程可能更加高效。 ... [详细]
  • 开发笔记:实验7的文件读写操作
    本文介绍了使用C++的ofstream和ifstream类进行文件读写操作的方法,包括创建文件、写入文件和读取文件的过程。同时还介绍了如何判断文件是否成功打开和关闭文件的方法。通过本文的学习,读者可以了解如何在C++中进行文件读写操作。 ... [详细]
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社区 版权所有