pedroSG94 / RootEncoder

RootEncoder for Android (rtmp-rtsp-stream-client-java) is a stream encoder to push video/audio to media servers using protocols RTMP, RTSP, SRT and UDP with all code written in Java/Kotlin
Apache License 2.0
2.45k stars 761 forks source link

How to play multiple files with RtspServerFromFile #1487

Open hjywyj opened 1 month ago

hjywyj commented 1 month ago

I have four mp4 files: record1.mp4, record2.mp4, record3.mp4, record4.mp4 Currently I want to implement it, given a fileList=[record1.mp4,record2.mp4] Use RtspServerFromFile to ignore the first 15 seconds from record1.mp4 and start the output stream. After record1.mp4 has finished playing, continue to record2.mp4. How should it be implemented and prepareVideo again?

pedroSG94 commented 1 month ago

Hello,

Currently it is not supported. To do that you need stop stream totally and start again with the new video file. The reason to no support this is because you could use different video/audio file config and in this case you can't handle with that. For example: If you use 2 files totally different like 1280x720 video with audio 44100 sample rate stereo channel and other file with 640x480 video and audio 32000 mono channel. You can't continue with the second file because you have the stream and encoders configured for the first file. I'm not totally sure how to handle this feature so it is not developed for now.

Should I develop the way to do it but only supported files with the same configuration?

pedroSG94 commented 1 month ago

For now, you can try join all files in one (you will need find a library for it) and then use the result in the library to stream

hjywyj commented 1 month ago

I'm sorry, I didn't take into account the problem of different bitrates, but are files with the same bitrate OK? Your library is great. I used it to record video surveillance for 24 hours and save the file every hour. The problem we are currently encountering is that multiple video files are pushed to one stream and can only be interrupted and then started again. So I would like to ask if there is any smoother switching method.

hjywyj commented 1 month ago

For now, you can try join all files in one (you will need find a library for it) and then use the result in the library to stream

This is a good approach, but it consumes storage space.

pedroSG94 commented 1 month ago

For now, you can try join all files in one (you will need find a library for it) and then use the result in the library to stream

This is a good approach, but it consumes storage space.

Yes, but this is the only way for now. I will mark this issue as enhancement and develop the way to stream multiples files but only if that files are identical in config (video and audio). The files will require:

I'm not sure if throw a crash in prepareVideo or prepareAudio if you are using different files with different config or just return false in the method. What do you think is a better idea?

hjywyj commented 1 month ago

For now, you can try join all files in one (you will need find a library for it) and then use the result in the library to stream

This is a good approach, but it consumes storage space.

Yes, but this is the only way for now. I will mark this issue as enhancement and develop the way to stream multiples files but only if that files are identical in config (video and audio). The files will require:

  • Same video resolution, audio samplerate and audio channels

I'm not sure if throw a crash in prepareVideo or prepareAudio if you are using different files with different config or just return false in the method. What do you think is a better idea?

Yes, I think it's a better idea.I can't think of any other better solution. Thanks!

pedroSG94 commented 1 week ago

Hello,

This feature was added to RootEncoder in the last commit. In the next release I will add this feature to RTSP-Server

Usage example:

hjywyj commented 1 week ago

Hello, Thank you very much I think I need to study the "reSyncFile" method. I am not sure if it will cause video stream lag due to high time consumption when running on low-end devices. Thank you again

pedroSG94 commented 1 week ago

reSyncFile is optional but I recommend you use it if you can after change the file. You can use it only on high-end devices if you want. Also you can check the time in audio and video and only resync if the difference is high. Something like this:

val vTime = genericFromFile.getVideoTime()
val aTime = genericFromFile.getAudioTime()
if (abs(vTime - aTime) >= 0.5) { //async by 0.5s
genericFromFile.reSyncFile()
}

0.5 is a random value (in seconds), you should check the values and use a value that adjust to you.