forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoContent.cs
More file actions
143 lines (121 loc) · 4.54 KB
/
Copy pathVideoContent.cs
File metadata and controls
143 lines (121 loc) · 4.54 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// MonoGame - Copyright (C) MonoGame Foundation, Inc
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.
using Microsoft.Xna.Framework.Media;
using System.Globalization;
using MonoGame.Tool;
namespace Microsoft.Xna.Framework.Content.Pipeline
{
/// <summary>
/// Provides a base class for all video objects.
/// </summary>
public class VideoContent : ContentItem, IDisposable
{
private bool _disposed;
/// <summary>
/// Gets the bit rate for this video.
/// </summary>
public int BitsPerSecond { get; }
/// <summary>
/// Gets the duration of this video.
/// </summary>
public TimeSpan Duration { get; }
/// <summary>
/// Gets or sets the file name for this video.
/// </summary>
[ContentSerializer]
public string Filename { get; set; }
/// <summary>
/// Gets the frame rate for this video.
/// </summary>
public float FramesPerSecond { get; }
/// <summary>
/// Gets the height of this video.
/// </summary>
public int Height { get; }
/// <summary>
/// Gets or sets the type of soundtrack accompanying the video.
/// </summary>
[ContentSerializer]
public VideoSoundtrackType VideoSoundtrackType { get; set; }
/// <summary>
/// Gets the width of this video.
/// </summary>
public int Width { get; }
/// <summary>
/// Initializes a new copy of the VideoContent class for the specified video file.
/// </summary>
/// <param name="filename">The file name of the video to import.</param>
public VideoContent(string filename)
{
ArgumentException.ThrowIfNullOrEmpty(filename);
if (!File.Exists(filename))
throw new FileNotFoundException(filename);
Filename = filename;
var result = FFprobe.Run($"-i \"{Filename}\" -show_format -select_streams v -show_streams -print_format ini", out var stdout, out var stderr);
if (result != 0)
{
throw new Exception($"ffprobe exited with {result}:\n{stderr}");
}
var lines = stdout.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
if (!line.Contains('='))
continue;
var key = line[..line.IndexOf('=')];
var value = line[(line.IndexOf('=') + 1)..];
switch (key)
{
case "duration":
Duration = TimeSpan.FromSeconds(double.Parse(value, CultureInfo.InvariantCulture));
break;
case "bit_rate":
BitsPerSecond = int.Parse(value, CultureInfo.InvariantCulture);
break;
case "width":
Width = int.Parse(value, CultureInfo.InvariantCulture);
break;
case "height":
Height = int.Parse(value, CultureInfo.InvariantCulture);
break;
case "r_frame_rate":
var frac = value.Split('/');
FramesPerSecond = float.Parse(frac[0], CultureInfo.InvariantCulture) / float.Parse(frac[1], CultureInfo.InvariantCulture);
break;
}
}
}
/// <summary/>
~VideoContent()
{
Dispose(false);
}
/// <summary>
/// Immediately releases the unmanaged resources used by this object.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <inheritdoc cref="Dispose()"/>
/// <param name="disposing">
/// <see langword="true"/> to release both managed and unmanaged resources;
/// <see langword="false"/> to release only unmanaged resources.
/// </param>
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
// TODO: Free managed resources here
// ...
}
// TODO: Free unmanaged resources here
// ...
_disposed = true;
}
}
}
}