opencv videocapture 从ts文件获取h264
时间: 2023-11-18 10:03:58 浏览: 402
要使用OpenCV VideoCapture从TS文件获取H264视频,您需要先使用FFmpeg将TS文件转换为H264格式。以下是一些步骤:
1. 安装FFmpeg:
```bash
sudo apt-get install ffmpeg
```
2. 将TS文件转换为H264格式:
```bash
ffmpeg -i input.ts -vcodec copy -bsf:v h264_mp4toannexb -an output.h264
```
3. 使用OpenCV VideoCapture从H264文件中读取视频帧:
```python
import cv2
cap = cv2.VideoCapture('output.h264')
while True:
ret, frame = cap.read()
if not ret:
break
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
```
请注意,如果您的H264视频帧未经过编码,则您需要使用OpenCV中的VideoWriter来将其编码为视频文件。
相关问题
opencv打开ts流
OpenCV是一个开源的计算机视觉库,它提供了许多用于图像和视频处理的函数和工具。要打开TS流(Transport Stream),可以使用OpenCV中的VideoCapture类来实现。
首先,你需要安装OpenCV库并确保它已经正确配置在你的开发环境中。然后,你可以按照以下步骤来打开TS流:
1. 创建一个VideoCapture对象,并传入TS流的路径作为参数:
```cpp
cv::VideoCapture cap("path_to_ts_stream.ts");
```
2. 检查是否成功打开了TS流:
```cpp
if (!cap.isOpened()) {
// 打开TS流失败
return -1;
}
```
3. 循环读取TS流中的帧数据:
```cpp
cv::Mat frame;
while (cap.read(frame)) {
// 在这里对每一帧进行处理
// ...
}
```
在循环中,你可以对每一帧进行处理,例如显示、保存或进行其他图像处理操作。
注意:在使用完毕后,记得释放VideoCapture对象:
```cpp
cap.release();
```
基于ffmpeg SDK 和opencv,使用c++语言编写一个函数,将一张图片编码成H264格式推流到本地的rtsp服务器上
可以通过以下代码实现要求:
```
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include <unistd.h>
#include <libavcodec/avcodec.h>
#include <libavutil/imgutils.h>
#include <libavutil/opt.h>
#include <libavutil/mathematics.h>
#include <libavutil/time.h>
#include <libswscale/swscale.h>
#include <libavformat/avformat.h>
#include <opencv2/opencv.hpp>
using namespace cv;
AVCodecContext *codec_ctx = NULL;
AVCodec *codec = NULL;
AVFrame *frame = NULL;
AVPacket *pkt = NULL;
SwsContext* img_convert_ctx = NULL;
int video_stream_index = -1;
AVFormatContext *fmt_ctx = NULL;
// 初始化ffmpeg和opencv相关的数据结构
void init(const char *url, int width, int height, int fps)
{
av_register_all();
avcodec_register_all();
avformat_network_init();
// 创建输出格式上下文
avformat_alloc_output_context2(&fmt_ctx, nullptr, "rtsp", url);
if (!fmt_ctx)
{
printf("Could not create output context.\n");
exit(1);
}
// 添加视频流
codec = avcodec_find_encoder_by_name("libx264");
if (!codec)
{
printf("Codec not found.\n");
exit(1);
}
video_stream_index = avformat_new_stream(fmt_ctx, codec);
if (video_stream_index < 0)
{
printf("Could not allocate stream.\n");
exit(1);
}
codec_ctx = avcodec_alloc_context3(codec);
if (!codec_ctx)
{
printf("Could not allocate codec context.\n");
exit(1);
}
// 设置视频的宽、高、帧率等参数
codec_ctx->bit_rate = 400000;
codec_ctx->width = width;
codec_ctx->height = height;
codec_ctx->time_base.num = 1;
codec_ctx->time_base.den = fps;
codec_ctx->gop_size = 12;
codec_ctx->max_b_frames = 1;
codec_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
// 打开编码器
avcodec_open2(codec_ctx, codec, NULL);
// 分配AVFrame
frame = av_frame_alloc();
if (!frame)
{
printf("Could not allocate video frame.\n");
exit(1);
}
frame->format = codec_ctx->pix_fmt;
frame->width = codec_ctx->width;
frame->height = codec_ctx->height;
// 分配AVPacket
pkt = av_packet_alloc();
if (!pkt)
{
printf("Could not allocate packet.\n");
exit(1);
}
// 初始化SWSContext
img_convert_ctx = sws_getContext(width, height, AV_PIX_FMT_BGR24,
width, height, AV_PIX_FMT_YUV420P,
0, nullptr, nullptr, nullptr);
if (!img_convert_ctx)
{
printf("Could not initialize conversion context.\n");
exit(1);
}
// 通过调用avio_open2()函数打开输出文件
if (!(fmt_ctx->flags & AVFMT_NOFILE))
{
int ret = avio_open2(&fmt_ctx->pb, url, AVIO_FLAG_WRITE, nullptr, nullptr);
if (ret < 0)
{
printf("Could not open '%s'.\n", url);
exit(1);
}
}
// 通过调用avformat_write_header()函数写入文件头
int ret = avformat_write_header(fmt_ctx, nullptr);
if (ret < 0)
{
printf("Could not write header.\n");
exit(1);
}
}
// 推流函数,将一帧图像推送到rtsp服务器
void push_frame(Mat &frame)
{
// 格式转换:将OpenCV中的BGR格式转换成YUV420P格式
AVFrame *av_frame = av_frame_alloc();
uint8_t *data[AV_NUM_DATA_POINTERS] = {0};
int linesize[AV_NUM_DATA_POINTERS] = {0};
av_frame->data[0] = frame.data;
av_frame->linesize[0] = frame.cols * 3;
av_frame->format = AV_PIX_FMT_BGR24;
av_frame->width = frame.cols;
av_frame->height = frame.rows;
sws_scale(img_convert_ctx, av_frame->data, av_frame->linesize,
0, frame.rows, frame->data, frame->linesize);
// 初始化AVFrame
av_frame = av_frame_alloc();
if (!av_frame)
{
printf("Could not allocate video frame.\n");
exit(1);
}
av_frame->format = codec_ctx->pix_fmt;
av_frame->width = codec_ctx->width;
av_frame->height = codec_ctx->height;
ret = av_frame_get_buffer(av_frame, 32);
if (ret < 0)
{
printf("Could not allocate the video frame data.\n");
exit(1);
}
// 编码一帧视频
ret = avcodec_send_frame(codec_ctx, av_frame);
if (ret < 0)
{
printf("Error sending a frame for encoding.\n");
exit(1);
}
while (ret >= 0)
{
ret = avcodec_receive_packet(codec_ctx, pkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
return;
else if (ret < 0)
{
printf("Error during encoding.\n");
exit(1);
}
// 将编码后的帧写入文件
av_packet_rescale_ts(pkt, codec_ctx->time_base, fmt_ctx->streams[video_stream_index]->time_base);
pkt->stream_index = video_stream_index;
ret = av_interleaved_write_frame(fmt_ctx, pkt);
if (ret < 0)
{
printf("Error writing video frame.\n");
exit(1);
}
}
}
int main(int argc, char **argv)
{
const char *url = "rtsp://127.0.0.1:8554/test.sdp";
int width = 640;
int height = 480;
int fps = 30;
init(url, width, height, fps);
Mat image;
VideoCapture cap(0);
if (!cap.isOpened())
{
printf("Could not initialize capturing...\n");
return -1;
}
while (1)
{
cap >> image;
if (image.empty())
break;
push_frame(image);
}
av_write_trailer(fmt_ctx);
// 释放资源
sws_freeContext(img_convert_ctx);
avcodec_free_context(&codec_ctx);
av_frame_free(&frame);
av_packet_free(&pkt);
avformat_free_context(fmt_ctx);
return 0;
}
```
该函数使用了OpenCV库来获取摄像头的视频流,使用ffmpeg库将视频流编码成H264格式,并推流到本地的rtsp服务器上。
阅读全文
相关推荐
















