forked from TensorStack-AI/TensorStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoInputStream.cs
More file actions
105 lines (92 loc) · 4.43 KB
/
VideoInputStream.cs
File metadata and controls
105 lines (92 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (c) TensorStack. All rights reserved.
// Licensed under the Apache 2.0 License.
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using TensorStack.Common;
using TensorStack.Common.Tensor;
using TensorStack.Common.Video;
namespace TensorStack.Video
{
public class VideoInputStream
{
private readonly string _filename;
private readonly string _videoCodec;
private readonly VideoInfo _videoInfo;
/// <summary>
/// Initializes a new instance of the <see cref="VideoInputStream"/> class.
/// </summary>
/// <param name="filename">The filename.</param>
/// <param name="videoCodec">The video codec.</param>
/// <exception cref="System.Exception">Failed to open video file.</exception>
public VideoInputStream(string filename, string videoCodec = "mp4v")
{
_filename = filename;
_videoCodec = videoCodec;
_videoInfo = VideoService.LoadVideoInfo(_filename);
}
/// <summary>
/// Gets the filename.
/// </summary>
/// <value>The filename.</value>
public string Filename => _filename;
/// <summary>
/// Gets the video width.
/// </summary>
public int Width => _videoInfo.Width;
/// <summary>
/// Gets the video height.
/// </summary>
public int Height => _videoInfo.Height;
/// <summary>
/// Gets the video frame rate.
/// </summary>
public float FrameRate => _videoInfo.FrameRate;
/// <summary>
/// Gets the video frame count.
/// </summary>
public int FrameCount => _videoInfo.FrameCount;
/// <summary>
/// Gets the VideoFrame stream.
/// </summary>
/// <param name="frameRate">The frame rate.</param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="cancellationToken">The cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <returns>IAsyncEnumerable<ImageFrame>.</returns>
public IAsyncEnumerable<VideoFrame> GetAsync(int? width = default, int? height = default, float? frameRate = default, ResizeMode resizeMode = ResizeMode.Stretch, CancellationToken cancellationToken = default)
{
return VideoService.ReadStreamAsync(_filename, frameRate, width, height, resizeMode, cancellationToken);
}
/// <summary>
/// Saves the VideoFrame stream.
/// </summary>
/// <param name="filename">The filename.</param>
/// <param name="stream">The stream.</param>
/// <param name="frameRate">The frame rate.</param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="cancellationToken">The cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <returns>Task.</returns>
public Task SaveAsync(IAsyncEnumerable<VideoFrame> stream, string filename, int? width = default, int? height = default, float? frameRate = default, CancellationToken cancellationToken = default)
{
return VideoService.WriteVideoStreamAsync(stream, filename, _videoCodec, frameRate, width, height, cancellationToken);
}
/// <summary>
/// Create a buffered VideoTensor of this stream.
/// </summary>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="frameRate">The frame rate.</param>
/// <param name="cancellationToken">The cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <returns>A Task<VideoTensor> representing the asynchronous operation.</returns>
public async Task<VideoTensor> CreateTensorAsync(int? width = default, int? height = default, float? frameRate = default, ResizeMode resizeMode = ResizeMode.Stretch, CancellationToken cancellationToken = default)
{
var buffered = await GetAsync(width, height, frameRate, resizeMode, cancellationToken)
.Select(x => x.Frame)
.ToArrayAsync(cancellationToken);
return new VideoTensor(buffered.Join(), frameRate ?? FrameRate);
}
}
}