android h264转mp4文件,Android - Save an rtsp (h264) stream to mp4 file

博主正在尝试从IP摄像头的RTSP流中读取H.264编码的视频并将其录制为MP4文件。已成功显示流,但遇到了录制问题。考虑了MediaRecorder和MediaCodec两个选项,但MediaRecorder似乎不支持RTSP输入,而MediaExtractor与MediaCodec组合使用时遇到了异常。寻求代码片段帮助,考虑直接从URL读取并存储为文件,或者寻找其他可行方案。FFMPEG由于性能问题已被排除。

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

I am working on a project where I need to

Read input H.264 encoded stream from an IPCamera - I am able to fetch this in through an rtsp url like rtsp://192.168.1.83:8001/

Display the IPCamera stream - This I am able to do using the

final VideoView vv = (VideoView) findViewById(R.id.video_view_h264);

MediaController mc = new MediaController(getApplicationContext());

vv.setVideoURI(video);

vv.setMediaController(mc);

vv.requestFocus();

vv.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

public void onPrepared(MediaPlayer mp) {

vv.start();

}

});

Now I want to record this stream to an MP4 file. This is where I am stuck and I am considering the following options

a) MediaRecorder - Based on my google searches I believe for this class the input can only from a device camera. Is there a way to tweak this where I can provide an input from rtsp stream ?

b) MediaCodec API - 4.1 onwards Android has released this low level API with and MediaExtractor and MediaCodec. For this option I think an rtsp stream cannot be used in the following snippet

final String STREAM_URL = "rtsp://192.168.1.83:8001/";

MediaExtractor mediaExtractor = new MediaExtractor();

mediaExtractor.setDataSource(STREAM_URL); // I get an exception 04-28 18:30:18.914: E/AndroidRuntime(8140): Caused by: java.io.IOException: Failed to instantiate extractor.

c) Can I do a read from the url and store it like a file. How to I convert this stream to an MP4 file ? Any code snippet will be really helpful.

I had also tried to use FFMPEG but the performance was so poor that I dropped this option.

Any inputs on the above three options or any other additional option which I can consider will be greatly appreciated.

Thanks !!

### 解决Linux环境下RTSP启动时的错误问题 在Linux环境中,当尝试通过FFmpeg或其他工具启动RTSP流服务时,可能会遇到各种类型的错误。这些错误通常涉及编译阶段的问题、依赖库缺失或配置不当等情况。 #### 可能的原因分析 1. **缺少必要的编码器支持** 如果在编译过程中出现了类似于`implicit declaration of function 'deflateBound'`这样的警告并被当作错误处理,则可能是由于未正确安装Zlib库及其开发头文件所致[^2]。这表明系统缺乏某些必需的功能声明或者链接选项不完整。 2. **权限不足** RTSP服务器可能需要特定端口(如554)来运行,而低编号端口通常仅允许root用户绑定。如果以普通用户身份执行程序却试图监听此类特权端口,则会失败。 3. **网络设置冲突** 防火墙规则、SELinux策略或者其他网络安全措施也可能阻止RTSP流量正常传输。此外,本地主机名解析异常也会影响客户端连接到本机上的RTSP服务。 4. **软件版本兼容性** 不同版本之间的API变更可能导致旧版应用程序无法顺利操作新版协议栈中的组件。例如,在较新的GStreamer构建里弃用了部分早先广泛使用的插件接口定义。 #### 推荐解决方案 为了有效应对上述提到的各种潜在障碍,可以采取如下措施: - 安装所有必备的支持包之前重新清理源码树再继续进行全新一轮完整的构建流程: ```bash sudo apt-get update && sudo apt-get install build-essential checkinstall cmake pkg-config yasm \ libjpeg-dev libjasper-dev libpng12-dev libtiff5-dev libdc1394-22-dev libavcodec-dev \ libavformat-dev libswscale-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev zlib1g-dev \ libsdl2-dev libopencore-amrnb-dev libopencore-amrwb-dev libfaac-dev libmp3lame-dev libspeex-dev \ libopenal-dev libvo-aacenc-dev libvo-amrwbenc-dev libqt4-dev python-numpy python-scipy python-matplotlib ipython python-pygame cd /path/to/your/source/directory/ make distclean || true ; ./configure --enable-gpl --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-nonfree --extra-cflags=-I/usr/local/include --extra-ldflags=-L/usr/local/lib time make -j$(nproc) checkinstall ``` - 调整防火墙设定以便放行指定范围内的数据交换活动: ```bash iptables -A INPUT -p tcp --dport 8000:9000 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT service iptables save systemctl restart firewalld.service setenforce Permissive semanage port -a -t rtsp_port_t -p tcp {8000..9000} ``` - 使用绝对路径调用可执行二进制文件,并验证环境变量是否已正确定义: ```bash export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH which ffmpeg ffplay ffprobe ffserver -f ~/rtsp.conf & sleep 2s ffplay rtmp://localhost/live/stream_key ``` #### 示例代码片段展示如何创建简单的RTSP播放脚本 下面给出了一段Python脚本来演示利用OpenCV读取来自远程地址的实时视频帧序列过程: ```python import cv2 cap = cv2.VideoCapture('rtsp://username:password@ip_address:port/path') if not cap.isOpened(): print("Error opening video stream or file") while True: ret, frame = cap.read() if ret: gray_frame = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) cv2.imshow('Grayscale Video',gray_frame) key=cv2.waitKey(1)&0xFF if key==ord('q'): break else : break cap.release() cv2.destroyAllWindows() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值