C ++视频流和transimisson [英] C++ Video streaming and transimisson

查看:220
本文介绍了C ++视频流和transimisson的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的视频解码/编码。目前我有一个任务来测试网络编码的视频传输。网络编码程序已经完成。

I am new to video decoding/encoding. Currently I have a task to test the video transmission for a network coding. The network coding programme was already done.

首先,我试图将视频分割成opencv中的帧,然后传输帧,但分割后,我发现一个3MB的视频转换为80MB总大小的帧!这对于传输不是有效的。有没有更好的方式来做视频传输?任何专业人员能给我提供一个C ++示例代码吗?我被告知,由于传输带宽限制,不能直接将视频放入缓冲区。我想知道使用帧间差异减少传输文件大小,但我不知道ht这样做在C + +。

Firstly I tried to divide the video into frames in opencv, and transmit the frames, but after division, I found a 3MB video are converted to 80MB total size frames!! which is not efficient for transmission. Is there any better way to do the video transmission? Can any pros provide me a sample code in C++? I be told that cannot directly put video into buff due to the transmission bandwidth limitation. I am wondering using the inter frame difference to reduce the transmission file size but I do not know ht to do this in C++.

这里是我的代码视频分成帧。

Here is my code for video divide into frames.

#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{

VideoCapture cap("/home/yonghao/Documents/50MbitMJPEG1080p.mp4"); // open the video file for reading


double fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video
int numFrames = cap.get(CV_CAP_PROP_FRAME_COUNT); // get the total number of frames

 cout << "Frame per seconds : " << fps << endl;
 cout << "Total Frame Numbers : " << numFrames << endl;

namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"

int frame_number = 1;

while(frame_number<=numFrames)
{
Mat frame;

bool bSuccess = cap.read(frame); // read a new frame from video

if (!bSuccess) //if not success, break loop
{
    cout << "Cannot read the frame from video file" << endl;
    break;
}

    imshow("MyVideo", frame); //show the frame in "MyVideo" window

//save frame

    stringstream ss;
    string name = "/home/yonghao/Documents/Frames/frame_";
    string type = ".jpg";

    ss<<name<<(frame_number)<<type;

    string filename = ss.str();
    ss.str("");

imwrite(filename, frame);

cout << "Frame " << frame_number << " has been generated." << endl;

frame_number++;

//user exit by press ESC button

if(waitKey(30) == 27) //wait for 'esc' key press for 30 ms. If 'esc' key is pressed,    break loop
{
        cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}


推荐答案

如果视频的比特率对于你的带宽来说太高了,你需要重新压缩它,使用标准的视频编解码器,例如h264。
你可以使用ffmpeg程序,使用类似:

If the bitrate of your video is too high for your bandwidth, you need to recompress it, using a standard video codec, for example h264. You can do that with the ffmpeg program, using something like :

ffmpeg -i <your_input_file> -c:v libx264 -crf 22 video.avi

这将使用默认选项和恒定质量(crf参数,越低越高的质量),请参见此处的选项h264编码器,在您的情况下,您可能更喜欢使用恒定的比特率,而不是恒定的质量。

This will encode with default options and a constant quality (crf parameter, the lower the higher quality), see here for the options of h264 encoder, in your case you may prefer to use constant bitrate instead of constant quality.

以正确的速度读取视频并在网络上流式传输可能在c使用libavformat库(ffmpeg库的一部分),但是对于初学者可能很复杂。

Reading the video at correct speed and streaming it on the network would be possible in c with libavformat library (part of ffmpeg libraries), but probably complex for a beginner.

一个更简单的解决方案是将ffmpeg命令的输出传递到您的程序网络编码。

One simpler solution would be to pipe the output of a ffmpeg command to your program that does the network coding.

例如:

ffmpeg -re -i input.avi -f rawvideo -c:v copy pipe:1 | ./network_encode

这将读取编码的视频文件,并将原始视频流发送到您的网络编码程序(这里network_encode从标准输入读取数据,应用您的网络编码并发送它)

This will read the encoded video file and send the raw video stream to your network encoding program (here network_encode read data from standard input, apply your network coding and send it)

在接收器上,您可以做类似的操作来可视化您的视频来自网络的数据,解码和将结果写入标准输出):

On the receiver, you can do something similar to visualize your video (here network_decode receive the data from the network, decode it, and write result to standard output):

./network_decode | ffplay -i -

这篇关于C ++视频流和transimisson的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆