Skip to content

Instantly share code, notes, and snippets.

@clogwog
Created March 9, 2023 05:08
Show Gist options
  • Save clogwog/b9ac21abf6acf4849fd5284c148034c9 to your computer and use it in GitHub Desktop.
Save clogwog/b9ac21abf6acf4849fd5284c148034c9 to your computer and use it in GitHub Desktop.
Load some MP4 data between 2 datetime stamps from AWS Kinesis Video Stream
using Amazon;
using Amazon.Runtime;
using Amazon.KinesisVideo;
using Amazon.KinesisVideo.Model;
using Amazon.KinesisVideoArchivedMedia;
using Amazon.KinesisVideoArchivedMedia.Model;
using System.Configuration;
public Stream LoadStream(string streamName, DateTime from, DateTime to)
{
MemoryStream ret_value = new MemoryStream();
DateTime fromUtc = DateTime.SpecifyKind(from, DateTimeKind.Utc);
DateTime toUtc = DateTime.SpecifyKind(to, DateTimeKind.Utc);
string accessKey = null;
string secretKey = null;
string regionStr = ConfigurationManager.AppSettings["dvr_kinesis_region"];
try
{
accessKey = ConfigurationManager.AppSettings["dvr_kinesis_access_key"];
secretKey = ConfigurationManager.AppSettings["dvr_kinesis_secret_key"];
}
catch { }
RegionEndpoint awsRegion = RegionEndpoint.GetBySystemName(regionStr);
AmazonKinesisVideoClient client1 = null;
if (!string.IsNullOrEmpty(accessKey) && !string.IsNullOrEmpty(secretKey))
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
client1 = new AmazonKinesisVideoClient(credentials, awsRegion);
}
else
{
client1 = new AmazonKinesisVideoClient(awsRegion);
}
var endpointResponse = client1.GetDataEndpoint(new GetDataEndpointRequest
{
APIName = new APIName("GET_CLIP"),
StreamName = streamName
});
var config = new AmazonKinesisVideoArchivedMediaConfig
{
RegionEndpoint = awsRegion,
ServiceURL = endpointResponse.DataEndpoint
};
AmazonKinesisVideoArchivedMediaClient client = null;
if (!string.IsNullOrEmpty(accessKey) && !string.IsNullOrEmpty(secretKey))
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
client = new AmazonKinesisVideoArchivedMediaClient(credentials, config);
}
else
{
client = new AmazonKinesisVideoArchivedMediaClient(config);
}
var getClipRequest = new GetClipRequest
{
StreamName = streamName,
ClipFragmentSelector = new ClipFragmentSelector
{
FragmentSelectorType = ClipFragmentSelectorType.PRODUCER_TIMESTAMP,
TimestampRange = new ClipTimestampRange
{
StartTimestamp = fromUtc.ToLocalTime(),
EndTimestamp = toUtc.ToLocalTime()
},
}
};
using (var response = client.GetClip(getClipRequest))
{
Console.WriteLine($" resusult: {response.HttpStatusCode} content lenght: {response.ContentLength} response sizeL {response.Payload.ToString()}");
response.Payload.CopyTo(ret_value);
ret_value.Seek(0, SeekOrigin.Begin);
return ret_value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment