2.RTSP客户端接收存储数据并转化为H264文件格式

有网友在testRTSPClient基础上,把接收的数据写成h264文件了

void DummySink::afterGettingFrame(void* clientData, unsigned frameSize, unsigned numTruncatedBytes,
                  struct timeval presentationTime, unsigned durationInMicroseconds) {
  DummySink* sink = (DummySink*)clientData; sink->afterGettingFrame(frameSize, numTruncatedBytes, presentationTime, durationInMicroseconds); } // If you don't want to see debugging output for each received frame, then comment out the following line: #define DEBUG_PRINT_EACH_RECEIVED_FRAME 1 void DummySink::afterGettingFrame(unsigned frameSize, unsigned numTruncatedBytes, struct timeval presentationTime, unsigned /*durationInMicroseconds*/) { // We've just received a frame of data. (Optionally) print out information about it: #ifdef DEBUG_PRINT_EACH_RECEIVED_FRAME if (fStreamId != NULL) envir() << "Stream \"" << fStreamId << "\"; "; envir() << fSubsession.mediumName() << "/" << fSubsession.codecName() << ":\tReceived " << frameSize << " bytes"; if (numTruncatedBytes > 0) envir() << " (with " << numTruncatedBytes << " bytes truncated)"; char uSecsStr[6+1]; // used to output the 'microseconds' part of the presentation time sprintf(uSecsStr, "%06u", (unsigned)presentationTime.tv_usec); envir() << ".\tPresentation time: " << (unsigned)presentationTime.tv_sec << "." << uSecsStr; if (fSubsession.rtpSource() != NULL && !fSubsession.rtpSource()->hasBeenSynchronizedUsingRTCP()) { envir() << "!"; // mark the debugging output to indicate that this presentation time is not RTCP-synchronized  } envir() << "\n"; #endif //todo one frame //save to file if(!strcmp(fSubsession.mediumName(), "video")) { if(firstFrame) { unsigned int num; SPropRecord *sps = parseSPropParameterSets(fSubsession.fmtp_spropparametersets(), num); // For H.264 video stream, we use a special sink that insert start_codes: struct timeval tv= {0,0}; unsigned char start_code[4] = {0x00, 0x00, 0x00, 0x01}; FILE *fp = fopen("test.264", "a+b"); if(fp) { fwrite(start_code, 4, 1, fp); fwrite(sps[0].sPropBytes, sps[0].sPropLength, 1, fp); fwrite(start_code, 4, 1, fp); fwrite(sps[1].sPropBytes, sps[1].sPropLength, 1, fp); fclose(fp); fp = NULL; } delete [] sps; firstFrame = False; } char *pbuf = (char *)fReceiveBuffer; char head[4] = {0x00, 0x00, 0x00, 0x01}; FILE *fp = fopen("test.264", "a+b"); if(fp) { fwrite(head, 4, 1, fp); fwrite(fReceiveBuffer, frameSize, 1, fp); fclose(fp); fp = NULL; } } // Then continue, to request the next frame of data:  continuePlaying(); } Boolean DummySink::continuePlaying() { if (fSource == NULL) return False; // sanity check (should not happen) // Request the next frame of data from our input source. "afterGettingFrame()" will get called later, when it arrives: fSource->getNextFrame(fReceiveBuffer, DUMMY_SINK_RECEIVE_BUFFER_SIZE, afterGettingFrame, this, onSourceClosure, this); return True; }
复制代码

### 回答1: 要使用QMediaPlayer读取RTSP数据并将其显示在QWidget上,您需要执行以下步骤: 1. 导入所需的模块: ```python from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent from PyQt5.QtWidgets import QWidget, QVBoxLayout, QApplication from PyQt5.QtCore import Qt, QUrl ``` 2. 创建QWidget和QVBoxLayout实例: ```python app = QApplication([]) widget = QWidget() layout = QVBoxLayout() widget.setLayout(layout) ``` 3. 创建QMediaPlayer实例并设置其media属性: ```python player = QMediaPlayer() media = QMediaContent(QUrl("rtsp://your-rtsp-stream-url")) player.setMedia(media) ``` 4. 创建视频显示区域并将其添加到QVBoxLayout: ```python video_widget = player.videoOutput() layout.addWidget(video_widget) ``` 5. 将QWidget显示出来: ```python widget.show() ``` 6. 设置QMediaPlayer的解码器,以确保支持h264格式: ```python player.setVideoCodec('h264') ``` 完整的示例代码如下: ```python from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent from PyQt5.QtWidgets import QWidget, QVBoxLayout, QApplication from PyQt5.QtCore import Qt, QUrl app = QApplication([]) widget = QWidget() layout = QVBoxLayout() widget.setLayout(layout) player = QMediaPlayer() media = QMediaContent(QUrl("rtsp://your-rtsp-stream-url")) player.setMedia(media) video_widget = player.videoOutput() layout.addWidget(video_widget) widget.show() player.setVideoCodec('h264') player.play() app.exec_() ``` 请注意,在使用RTSP流时,由于网络传输的原因,可能会有一些延迟。您可以使用QMediaPlayer的setPosition()方法设置播放器的位置以跳过某些部分,以减少延迟。 ### 回答2: 使用QMediaPlayer读取RTSP数据并展示到widget,以及设置h264格式播放,可以参考以下步骤: 首先,需要在Qt工程中引入Multimedia模块和Widgets模块。可以在.pro文件中添加如下行: ``` QT += multimedia widgets ``` 然后,在需要使用QMediaPlayer的地方,创建QMediaPlayer对象并设置相应的属性: ```cpp #include <QtMultimedia/QMediaPlayer> #include <QtMultimediaWidgets/QVideoWidget> // 创建QMediaPlayer对象 QMediaPlayer *player = new QMediaPlayer; // 创建QVideoWidget对象,用于展示视频 QVideoWidget *videoWidget = new QVideoWidget; // 设置QVideoWidget为QMediaPlayer的输出窗口 player->setVideoOutput(videoWidget); // 设置RTSP链接地址 QString rtspUrl = "rtsp://your_rtsp_url"; player->setMedia(QUrl(rtspUrl)); ``` 接下来,将QVideoWidget添加到Widget中,并设置相关布局和样式: ```cpp // 将QVideoWidget添加到Widget中 QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(videoWidget); ui->widget->setLayout(layout); // ui->widget指的是你想展示RTSP视频的widget // 设置QVideoWidget的样式 videoWidget->setStyleSheet("background-color:black"); ``` 最后,调用QMediaPlayer的play()函数开始播放RTSP视频: ```cpp player->play(); ``` 至此,通过QMediaPlayer就可以读取RTSP数据,并将视频展示到widget中。如果需要设置h264格式播放,可以在设置RTSP链接地址时,使用支持h264编码的RTSP链接地址: ```cpp QString rtspUrl = "rtsp://your_h264_rtsp_url"; player->setMedia(QUrl(rtspUrl)); ``` 注意,为了支持h264格式播放,确保编译环境中安装了支持h264解码的相关库。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值