简体   繁体   English

Android ffmpeg保存并附加h264流视频

[英]Android ffmpeg save and append h264 streamed videos

I need to save a video file generated by two video streams coming from two different sources. 我需要保存由来自两个不同来源的两个视频流生成的视频文件。 I'm using rtsp over tcp/ip, and the videos are encoded with h264. 我在tcp / ip上使用rtsp,并且视频使用h264编码。 I need to first record the video from the first source and than continue with the second source. 我需要先从第一个来源录制视频,然后再从第二个来源继续录制。 So what I tried was to declare two AVFormatContext instances, initialize both with avformat_open_input(&context, "rtsp://......",NULL,&options) and then read frames with av_read_frame(context,&packet) and write them in the video file av_write_frame(oc,&packet); 因此,我试图声明两个AVFormatContext实例,并使用avformat_open_input(&context, "rtsp://......",NULL,&options)初始化,然后使用av_read_frame(context,&packet)读取帧并将其写入视频文件av_write_frame(oc,&packet); It works fine saving the video from the first source, but if by example I saved y frames from the first context, when I try reading and saving the frames from the second context in the same file, for the first y frames I am tring to save, av_write_frame(oc,&packet2); 可以很好地保存来自第一个源的视频,但是例如,如果我从第一个上下文中保存了y个帧,那么当我尝试从同一文件中读取和保存第二个上下文中的帧时,我将转到第一个y帧保存, av_write_frame(oc,&packet2); would retun -22 , and would not add the frame to the file. 将重新调整-22 ,并且不会将帧添加到文件中。

I think the problem is that the context variable remembers how many frames were read, and it gives every read packet an identification number, to make sure it isn't written twice. 我认为问题在于上下文变量会记住读取了多少帧,并且为每个读取的数据包提供了一个标识号,以确保它不会被写入两次。 But when I'm using a new context those identification numbers reset, the AVOutputFormat or the AVFormatContext also retain the id of the package they are expecting to receive, and would not write anything until they receive a package with that id. 但是,当我使用新的上下文时,那些标识号将重置,AVOutputFormat或AVFormatContext也会保留他们期望接收的程序包的ID,并且在他们收到具有该ID的程序包之前不会编写任何内容。 Now I'm wondering how could I solve this inconvenience. 现在,我想知道如何解决此不便之处。 I can't find any setter for that id, or any way to reuse the same context. 我找不到该ID的任何设置方法,也找不到任何重用相同上下文的方法。 I thought to modify the ffmpeg sources but they are pretty complex and I couldn't find what I was looking for. 我本来想修改ffmpeg的资源,但是它们非常复杂,找不到我想要的东西。 An alternative would be to save the two video in two different files but, I don't know how to append them afterwards, as ffmpeg can only append videos encoded with mpeg and rencoding the video isn't really an option, as it will take to much time. 一种替代方法是将两个视频保存到两个不同的文件中,但是我不知道以后如何添加它们,因为ffmpeg只能添加以mpeg编码的视频,并且重新编码视频不是一个真正的选择,因为这将需要很多时间。 Also I couldn't find any other functional way to append two mp4 videos encoded with h264. 另外,我找不到其他功能来附加两个用h264编码的mp4视频。

I'll be happy to hear any kind of usable ideea to this problem. 我很高兴听到任何有关此问题的有用想法。

If you are saving raw h.264 streams why not simply store two seperate streams and then concatenate the file chunks on the command line seperately using a system command system("cat file1 file2 > finalfile") 如果要保存原始的h.264流,为什么不简单地存储两个独立的流,然后使用系统命令system(“ cat file1 file2> finalfile”)在命令行上分别串联文件块

If your output is one of the following you can append directly using cat 如果您的输出是以下之一,则可以直接使用cat追加

  1. Transport stream [ts] with same codecs 使用相同的编解码器传输流[ts]
  2. .mpg files .mpg文件
  3. raw h.264 files 原始的h.264文件
  4. raw mpeg4 files which have exactly same encoding headers [same dimensions, profile and toolsets mentioned in header] 具有完全相同的编码标头的原始mpeg4文件[标头中提到的相同尺寸,轮廓和工具集]
  5. H.263 streams H.263流

You cannot concatenate directly mp4 files or 3gpp files. 您不能直接串联mp4文件或3gpp文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM