简体   繁体   English

如何在 Java 中连接从 Kinesis Video Stream 获取的剪辑

[英]How to concatenate clips getting from Kinesis Video Stream in Java

I'm using AWS Kinesis Video Stream service to get my video recordings.我正在使用 AWS Kinesis Video Stream 服务来获取我的视频记录。 So due to the Kinesis Video Stream fragment limitation, it turns out I can only retrieve up to ~30 minutes video at one request.因此,由于 Kinesis Video Stream 片段限制,事实证明我一次请求最多只能检索约 30 分钟的视频。 And I was intend to retrieve a 2 hour video.我打算检索一个 2 小时的视频。

So I loop the request and get all 4 response into a List of InputStream , then I turn them into SequenceInputStream because I try to chain them all together.因此,我循环请求并将所有 4 个响应放入 List of InputStream ,然后将它们转换为SequenceInputStream因为我尝试将它们链接在一起。

However when I success uploaded them to S3 bucket and try to download from there.但是,当我成功将它们上传到 S3 存储桶并尝试从那里下载时。 It shows me file are corrupted.它显示我文件已损坏。 I researched on SequenceInputStream however it seems that my design was okay.我对SequenceInputStream进行了研究,但似乎我的设计还可以。

Furthermore, if I extend my video length, let say I have 24 InputStream , and I chained them all to a single SequenceInputStream , it will encounter the SSL Socket Exception: Connection Reset when I run the readAllBytes operation on the sequence input stream.此外,如果我扩展我的视频长度,假设我有 24 个InputStream ,并将它们全部链接到一个SequenceInputStream ,它将遇到SSL Socket Exception: Connection Reset当我对序列输入 stream 运行readAllBytes操作时,连接重置。

Is there any way I can achieve what I want or something wrong in my code to cause this?有什么方法可以实现我想要的或我的代码中的错误导致这种情况?

Here are my source code:这是我的源代码:

private String downloadMedia(Request request, JSONObject response, JSONObject metaData, Date startDate, Date endDate) throws Exception {
        long duration  = endDate.getTime() - startDate.getTime();
        long durationInMinutes = TimeUnit.MILLISECONDS.toMinutes(duration);
        long intervalsCount = durationInMinutes / 30;

        ArrayList<GetClipResult> getClipResults = new ArrayList<>();

        for (int i = 0; i < intervalsCount; i++){
            Media currentMedia = constructMediaAfterIntervalsBreakdown(metaData, request, startDate, endDate);

            String deviceName = metaData.getString("name") + "_" + request.getId();
            Stream stream = getStreamByName(name, request.getId());

            String endPoint = getDataEndpoint(stream.getStreamName());
            GetClipResult clipResult = downloadMedia(currentMediaDto, endPoint, stream.getStreamName());
            if(clipResult != null){
                getClipResults.add(clipResult);
            }

            startDate = currentMediaDto.getEndTime();
        }

        //Get presigned URL from S3 service response
        String url = response.getJSONArray("data").getJSONObject(0).getJSONArray("parts").getJSONObject(0).getString("url");

        if (getClipResults.size() > 0) {
            Vector<InputStream> inputStreams = new Vector<>();
            for (GetClipResult clipResult : getClipResults){
                InputStream videoStream = clipResult.getPayload();
                inputStreams.add(videoStream);
            }
            Enumeration<InputStream> inputStreamEnumeration = inputStreams.elements();;
            SequenceInputStream sequenceInputStream = new SequenceInputStream(inputStreamEnumeration);
            if (sequenceInputStream.available() > 0){
                sequenceInputStream.readAllBytes();
                byte[] bytes = sequenceInputStream.readAllBytes();
                String message = uploadFileUsingSecureUrl(url, bytes, metaData);
                return message;
            }
        }
        return "failed";
    }

Edited: I came across couple package that called Xuggler and FFMPEG , however most of them are getting the video file from disk (which has a path), but for my case there isn't any video file because I do not download them to local, they only existed in the runtime and will upload to S3 later on after concatenated.编辑:我遇到了一对名为XugglerFFMPEG的 package 夫妇,但是他们中的大多数都是从磁盘(有路径)获取视频文件,但就我而言,没有任何视频文件,因为我没有将它们下载到本地,它们只存在于运行时,连接后会上传到 S3。

Appreciates any help!感谢任何帮助! Thank you!谢谢!

So in the end I just downloaded the clips, saved it to the disk on runtime, merged them using mp4parser and upload to S3.所以最后我只是下载了剪辑,在运行时将其保存到磁盘,使用mp4parser合并它们并上传到 S3。 Afterwards I just deleted those on my disk.之后我只是删除了我磁盘上的那些。

If anyone curious about the code, it is taken from https://github.com/sannies/mp4parser/blob/master/examples/src/main/java/com/googlecode/mp4parser/AppendExample.java如果有人对代码感到好奇,它取自https://github.com/sannies/mp4parser/blob/master/examples/src/main/java/com/googlecode/mp4parser/AppendExample.java

Thank you.谢谢你。

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

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