亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

FFmpeg獲取網(wǎng)絡(luò)攝像頭數(shù)據(jù)解碼

 更新時(shí)間:2019年06月06日 11:07:25   作者:為取經(jīng)而來  
這篇文章主要為大家詳細(xì)介紹了FFmpeg獲取網(wǎng)絡(luò)攝像頭數(shù)據(jù)解碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

對(duì)USB攝像頭實(shí)時(shí)編碼,在前面已經(jīng)探討過了。這次改變下思路,嘗試去截取網(wǎng)絡(luò)攝像頭的H264碼流,將其解碼播放。

這里的測(cè)試代碼,是在??禂z像頭的基礎(chǔ)上進(jìn)行的。

解碼的大致流程和以前的保持一致,只不過增加了部分函數(shù)。

FFmpeg打開媒體文件并查看媒體文件的信息,有三個(gè)步驟:

avformat_open_input;

avformat_find_stream_info;

av_dump_format;

依次調(diào)用三個(gè)函數(shù)后,我們可以很清楚的知道碼流的各種信息。

完整的代碼:

#include <stdio.h>
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <windows.h>
#include "queue.h"
 
extern "C"
{
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
}
 
#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avutil.lib") 
#pragma comment(lib ,"swscale.lib")
 
using namespace std;
using namespace cv;
 
DWORD WINAPI opencv_imshow(LPVOID lparam)
{
 result_link_type* result_link = (result_link_type*)lparam;
 struct result_node_datatype *result_node2 = NULL;
 while (1)
 {
 result_node2 = result_pop(result_link);
 if (result_node2 == NULL)
 {
 Sleep(1);
 continue;
 }
 imshow("frame", result_node2->result);
 waitKey(1);
 }
}
 
int main(int argc, const char * argv[])
{
 HANDLE thread1;
 result_link_type *result_link = new result_link_type;
 result_link->head = result_link->end = NULL;
 result_link->result_num = 0;
 thread1 = CreateThread(NULL, 0, opencv_imshow, (LPVOID)result_link, 0, NULL);
 
 int i;
 int videoStream;
 int frameFinished;
 int numBytes;
 int ret;
 int got_picture;
 long prepts = 0;
 bool first_time = true;
 
 AVCodec *pCodec;
 AVFrame *pFrame;
 AVFrame *pFrameRGB;
 AVPacket packet;
 AVCodecContext *pCodecCtx;
 AVFormatContext *pFormatCtx = NULL;//結(jié)構(gòu)體AVFormatContext:包含碼流參數(shù)較多
 
 static struct SwsContext *img_convert_ctx;
 
 uint8_t *buffer;
 Mat pCvMat;
 
 char filepath[] = "rtsp://admin:jdh123456@10.170.6.187/axis-media/media.amp?camera=2";//碼流的獲取路徑
 
 av_register_all();//注冊(cè)編解碼器
 avformat_network_init();//加載socket庫以及網(wǎng)絡(luò)加密協(xié)議相關(guān)的庫
 
 if (avformat_open_input(&pFormatCtx, filepath, NULL, NULL) != 0)//打開多媒體數(shù)據(jù)并且獲得信息
 {
 return -1;
 }
 
 if (avformat_find_stream_info(pFormatCtx, NULL) < 0)//讀取視音頻數(shù)據(jù)并且獲得信息
 {
 return -1;
 }
 
 av_dump_format(pFormatCtx, 0, argv[1], false);//手工調(diào)試函數(shù),看到pFormatCtx->streams的內(nèi)容
 
 videoStream = -1;
 
 for (i = 0; i < pFormatCtx->nb_streams; i++)
 {
 if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
 {
 videoStream = i;
 break;
 }
 }
 
 if (videoStream == -1)
 {
 return -1;
 }
 
 pCodecCtx = pFormatCtx->streams[videoStream]->codec;
 
 pCodec = avcodec_find_decoder(pCodecCtx->codec_id);//查找解碼器
 
 if (pCodec == NULL)
 {
 return -1;
 }
 
 if (avcodec_open2(pCodecCtx, pCodec, 0) < 0)//初始化AVCodecContext
 {
 return -1;
 }
 
 if (pCodecCtx->time_base.num > 1000 && pCodecCtx->time_base.den == 1)
 {
 pCodecCtx->time_base.den = 1000;
 }
 
 pFrame = av_frame_alloc();//分配內(nèi)存
 pFrameRGB = av_frame_alloc();
 
 i = 0;
 while (1)
 {
 if (av_read_frame(pFormatCtx, &packet) >= 0)//讀取碼流中的音頻若干幀或者視頻一幀
 {
 if (packet.stream_index == videoStream)
 {
 ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, &packet);//開始解碼
 if (ret < 0)
 {
  printf("Decode Error.(解碼錯(cuò)誤)\n");
  return ret;
 }
 if (got_picture)//解碼成功,got_picture返回任意非零值
 {
  if (first_time)
  {
  //初始化SwsContext
  img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL, NULL);
  if (img_convert_ctx == NULL)
  {
  fprintf(stderr, "Cannot initialize the conversion context!\n");
  exit(1);
  }
 
  numBytes = avpicture_get_size(AV_PIX_FMT_BGR24, pCodecCtx->width, pCodecCtx->height);
  buffer = (uint8_t *)av_malloc(numBytes);
  avpicture_fill((AVPicture *)pFrameRGB, buffer, AV_PIX_FMT_BGR24, pCodecCtx->width, pCodecCtx->height); // allocator memory for BGR buffer 
  pCvMat.create(cv::Size(pCodecCtx->width, pCodecCtx->height), CV_8UC3);
  first_time = false;
  }
 
  //處理圖像數(shù)據(jù)
  sws_scale(img_convert_ctx, pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
  memcpy(pCvMat.data, buffer, numBytes);
  struct result_node_datatype *result_node = new struct result_node_datatype;
  result_node->result = pCvMat;
  result_push(result_link, result_node);
 }
 }
 av_free_packet(&packet);
 }
 }
 
 //free(buffer);
 av_free(buffer);
 av_free(pFrameRGB);
 av_free(pFrame);
 avcodec_close(pCodecCtx);
 avformat_close_input(&pFormatCtx);
 system("Pause");
 return 0;
}

隊(duì)列函數(shù):

#include "queue.h"
#include <iostream>
 
using namespace std;
 
void result_push(result_link_type* result_link, result_node_datatype * result_node) //入隊(duì)操作
{
 if (result_link->head == NULL)
 {
 result_link->head = result_node;
 result_link->end = result_link->head;
 result_link->result_num++;
 }
 else
 {
 result_link->end->next = result_node;
 result_link->end = result_node;
 result_link->result_num++;
 }
}
 
struct result_node_datatype* result_pop(result_link_type* result_link) //出隊(duì)操作
{
 struct result_node_datatype* tmp_node;
 if (result_link->head == NULL)
 return NULL;
 else if (result_link->head == result_link->end)
 {
 return NULL;
 }
 else
 {
 tmp_node = result_link->head;
 result_link->head = result_link->head->next;
 result_link->result_num--;
 return tmp_node;
 }
}

隊(duì)列函數(shù)的頭文件:

#ifndef QUEUE_H
#define QUEUE_H
#include <stdio.h>
#include <stdlib.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
 
typedef struct result_link_datatype
{
 struct result_node_datatype *head;
 struct result_node_datatype *end;
 int result_num;
}result_link_type;
 
struct result_node_datatype
{
 Mat result;
 struct result_node_datatype* next;
};
 
void result_push(result_link_type* result_link, result_node_datatype * result_node); //入隊(duì)操作
struct result_node_datatype* result_pop(result_link_type* result_link);//出隊(duì)操作
 
#endif

解碼后的數(shù)據(jù)進(jìn)入隊(duì)列,再從隊(duì)列中取出,利用opencv將其顯示(opencv顯示是另外開的一個(gè)線程函數(shù))。

admin:jdh123456@10.170.6.187,這里是攝像頭的名稱和IP地址。

測(cè)試代碼下載:點(diǎn)擊打開鏈接

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C語言排序方法(冒泡,選擇,插入,歸并,快速)

    C語言排序方法(冒泡,選擇,插入,歸并,快速)

    這篇文章給大家分享C語言所有經(jīng)典排序方法,文章給大家提供完整的實(shí)例代碼幫助大家快速學(xué)習(xí)掌握C語言排序方法,感興趣的朋友一起看看吧
    2021-08-08
  • C語言 數(shù)據(jù)結(jié)構(gòu)之中序二叉樹實(shí)例詳解

    C語言 數(shù)據(jù)結(jié)構(gòu)之中序二叉樹實(shí)例詳解

    這篇文章主要介紹了C語言 數(shù)據(jù)結(jié)構(gòu)之中序二叉樹實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • C++繼承詳細(xì)介紹

    C++繼承詳細(xì)介紹

    我們都知道面向?qū)ο笳Z言的三大特點(diǎn)是:**封裝,繼承,多態(tài)。**之前在類和對(duì)象部分,我們提到了C++中的封裝,那么今天呢,我們來學(xué)習(xí)一下C++中的繼承
    2022-10-10
  • c++中的消息框messagebox()詳細(xì)介紹及使用方法

    c++中的消息框messagebox()詳細(xì)介紹及使用方法

    本文將介紹下c++中的messagebox()的使用方法:常用屬性/按鈕的形式/返回值等等,感興趣的朋友可以了解下,希望本文可以幫助到你
    2013-02-02
  • VSCode 使用 Code Runner 插件無法編譯運(yùn)行文件名帶空格的文件問題

    VSCode 使用 Code Runner 插件無法編譯運(yùn)行文件名帶空格的文件問題

    這篇文章主要介紹了VSCode 使用 Code Runner 插件無法編譯運(yùn)行文件名帶空格的文件問題,本文通過圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-07-07
  • C語言利用cJSON解析JSON格式全過程

    C語言利用cJSON解析JSON格式全過程

    cJSON是用于解析json格式字符串的一套api,非常好用,下面這篇文章主要給大家介紹了關(guān)于C語言利用cJSON解析JSON格式的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-04-04
  • C語言 二叉樹的鏈?zhǔn)酱鎯?chǔ)實(shí)例

    C語言 二叉樹的鏈?zhǔn)酱鎯?chǔ)實(shí)例

    本篇文章主要介紹C語言中二叉樹的鏈?zhǔn)酱鎯?chǔ),這里提供了一個(gè)實(shí)例代碼進(jìn)行參考,這樣對(duì)二叉樹的鏈?zhǔn)酱鎯?chǔ)有更深入的了解,希望能幫到學(xué)習(xí)這塊知識(shí)的同學(xué)
    2016-07-07
  • 講解C語言編程中指針賦值的入門實(shí)例

    講解C語言編程中指針賦值的入門實(shí)例

    這篇文章主要介紹了講解C語言編程中指針賦值的入門實(shí)例,通過const int i與int *const pi這樣兩個(gè)例子來分析指針的賦值和地址指向,需要的朋友可以參考下
    2015-12-12
  • swift Character類型詳解及實(shí)例

    swift Character類型詳解及實(shí)例

    這篇文章主要介紹了 swift Character類型詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • C++生成隨機(jī)數(shù)的實(shí)現(xiàn)代碼

    C++生成隨機(jī)數(shù)的實(shí)現(xiàn)代碼

    這篇文章主要介紹了C++生成隨機(jī)數(shù)的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04

最新評(píng)論