forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VideoContent.cs
136 lines (116 loc) · 4.37 KB
/
VideoContent.cs
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
// MonoGame - Copyright (C) The MonoGame Team
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.
using System;
using System.Linq;
using Microsoft.Xna.Framework.Media;
using System.Globalization;
namespace Microsoft.Xna.Framework.Content.Pipeline
{
/// <summary>
/// Provides a base class for all video objects.
/// </summary>
public class VideoContent : ContentItem, IDisposable
{
private bool _disposed;
private int _bitsPerSecond;
private TimeSpan _duration;
private float _framesPerSecond;
private int _height;
private int _width;
/// <summary>
/// Gets the bit rate for this video.
/// </summary>
public int BitsPerSecond { get { return _bitsPerSecond; } }
/// <summary>
/// Gets the duration of this video.
/// </summary>
public TimeSpan Duration { get { return _duration; } }
/// <summary>
/// Gets or sets the file name for this video.
/// </summary>
[ContentSerializerAttribute]
public string Filename { get; set; }
/// <summary>
/// Gets the frame rate for this video.
/// </summary>
public float FramesPerSecond { get { return _framesPerSecond; } }
/// <summary>
/// Gets the height of this video.
/// </summary>
public int Height { get { return _height; } }
/// <summary>
/// Gets or sets the type of soundtrack accompanying the video.
/// </summary>
[ContentSerializerAttribute]
public VideoSoundtrackType VideoSoundtrackType { get; set; }
/// <summary>
/// Gets the width of this video.
/// </summary>
public int Width { get { return _width; } }
/// <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)
{
Filename = filename;
string stdout, stderr;
var result = ExternalTool.Run("ffprobe",
string.Format("-i \"{0}\" -show_format -select_streams v -show_streams -print_format ini", Filename), out stdout, out stderr);
var lines = stdout.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
if (!line.Contains('='))
continue;
var key = line.Substring(0, line.IndexOf('='));
var value = line.Substring(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;
}
}
}
~VideoContent()
{
Dispose(false);
}
/// <summary>
/// Immediately releases the unmanaged resources used by this object.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
// TODO: Free managed resources here
// ...
}
// TODO: Free unmanaged resources here
// ...
_disposed = true;
}
}
}
}