FFMPEG(一)--读取本地视频流和网络视频流

本文介绍了使用FFmpeg库从本地文件和网络流中读取视频的方法。通过示例代码展示了如何打开视频文件、获取视频信息及播放时长。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.读取本地视频流

// An highlighted block
#include<iostream>
using namespace std;
extern "C"
{
#include<libavcodec\avcodec.h>
#include<libavformat\avformat.h>
}
#pragma comment(lib,"avcodec.lib")
#pragma comment(lib,"avformat.lib")
int main()
{
	printf("%s\n", avcodec_configuration());

	av_register_all();

	AVFormatContext *pFormat = NULL;
	const char* path = "D:/vswork/ffmpeg/work/11.mp4";
	int ret=avformat_open_input(&pFormat,path,NULL,NULL);	//该函数用于打开多媒体数据并且获取一些信息
	//int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options);
	if (ret)
	{
		printf("avformat_open_input is faild\n");
		return -1;
	}
	printf("avformat_open_input succeed\n");

	ret=avformat_find_stream_info(pFormat,NULL);	//它可以从读取到的包中获得到流的信息
	//int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options);
	if (ret)
	{
		printf("avformat_find_stream_info is faild\n");
		return -1;
	}

	int time = pFormat->duration;
	int mbittime = (time / 1000000)/60;
	int mmintime = (time / 1000000) % 60;
	printf("%d分%d秒\n", mbittime, mmintime);
	av_dump_format(pFormat, NULL, path, 0);		//最后一个参数  0:打印输入流   1:打印输出流
	//void av_dump_format(AVFormatContext *ic,int index,const char *url,int is_output);

	system("pause");
	return 0;
}

在这里插入图片描述
2.读取网络视频流

// An highlighted block
#include<iostream>
using namespace std;

extern "C"
{
#include<libavcodec\avcodec.h>
#include<libavformat\avformat.h>
#include<libavutil\opt.h>
}
#pragma comment(lib,"avcodec.lib")
#pragma comment(lib,"avformat.lib")
#pragma comment(lib,"avutil.lib")

int main()
{
	//测试动态链接库是否成功
	printf("%s\n", avcodec_configuration());
	//注册dll
	av_register_all();
	//网络
	avformat_network_init();

	AVFormatContext *pFormat = NULL;

	//const char* path = "D:/vswork/ffmpeg/work/11.mp4";
	//http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8
	const char *path = "http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8";
	AVDictionary* opt = NULL;
	av_dict_set(&opt,"rtsp_transport","tcp",0);
	av_dict_set(&opt, "max_delay", "550", 0);

	int ret=avformat_open_input(&pFormat,path,NULL,&opt);	//该函数用于打开多媒体数据并且获取一些信息
	//int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options);
	if (ret)
	{
		printf("avformat_open_input is faild\n");
		return -1;
	}
	printf("avformat_open_input succeed\n");

	ret=avformat_find_stream_info(pFormat,NULL);	//它可以从读取到的包中获得到流的信息
	//int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options);
	if (ret)
	{
		printf("avformat_find_stream_info is faild\n");
		return -1;
	}

	int time = pFormat->duration;
	int mbittime = (time / 1000000)/60;
	int mmintime = (time / 1000000) % 60;
	printf("%d分%d秒\n", mbittime, mmintime);
	av_dump_format(pFormat, NULL, path, 0);		//最后一个参数  0:打印输入流   1:打印输出流
	//void av_dump_format(AVFormatContext *ic,int index,const char *url,int is_output);

	system("pause");
	return 0;
}

在这里插入图片描述

### 使用 `ffmpeg-python` 接收并处理 H265 视频流 为了使用 `ffmpeg-python` 来接收处理 H265 视频流,可以按照如下方式操作: #### 安装依赖库 确保已经安装了必要的 Python 库以及 FFmpeg 工具。可以通过 pip 安装 `ffmpeg-python` 并确认 FFmpeg 支持 H265 编解码器。 ```bash pip install ffmpeg-python ``` 验证 FFmpeg 是否支持 H265 可以通过命令行运行 `ffmpeg -version` 查看版本信息及其编译选项[^2]。 #### 创建脚本读取H265视频流 下面是个简单的例子来展示如何利用 `ffmpeg-python` 获取网络摄像机或其他源发送过来的 H265 流,并将其转换成图像帧以便进步分析或显示。 ```python import ffmpeg import numpy as np from PIL import Image def get_video_frame(url, width=1920, height=1080): """ 从给定 URL 中获取单个视频帧作为 NumPy 数组. 参数: url (str): 输入 RTSP 或其他协议的URL字符串. width (int): 输出图片宽度,默认为1920像素. height (int): 输出图片高度,默认为1080像素. 返回值: frame (numpy.ndarray): 图像数据数组. """ out, _ = ( ffmpeg.input(url) .filter('scale', size=f'{width}x{height}') .output('pipe:', format='rawvideo', pix_fmt='rgb24') .run(capture_stdout=True, capture_stderr=True) ) video = ( np.frombuffer(out, np.uint8) .reshape([height, width, 3]) ) return video if __name__ == '__main__': rtsp_url = "rtsp://example.com/h265_stream" img_array = get_video_frame(rtsp_url) # 将NumPy数组保存为PNG文件 image = Image.fromarray(img_array) image.save('./frame.png') print("Frame saved successfully.") ``` 此代码片段展示了如何定义函数 `get_video_frame()` 来连接到指定地址上的实时传输协议(RTSP)服务器,并从中抓取帧画面。这里假设输入的是个标准分辨率的 H265 流;如果实际应用中的编码参数不同,则可能需要调整相应的滤镜设置[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值