// 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 { /// /// Provides a base class for all video objects. /// public class VideoContent : ContentItem, IDisposable { private bool _disposed; private int _bitsPerSecond; private TimeSpan _duration; private float _framesPerSecond; private int _height; private int _width; /// /// Gets the bit rate for this video. /// public int BitsPerSecond { get { return _bitsPerSecond; } } /// /// Gets the duration of this video. /// public TimeSpan Duration { get { return _duration; } } /// /// Gets or sets the file name for this video. /// [ContentSerializerAttribute] public string Filename { get; set; } /// /// Gets the frame rate for this video. /// public float FramesPerSecond { get { return _framesPerSecond; } } /// /// Gets the height of this video. /// public int Height { get { return _height; } } /// /// Gets or sets the type of soundtrack accompanying the video. /// [ContentSerializerAttribute] public VideoSoundtrackType VideoSoundtrackType { get; set; } /// /// Gets the width of this video. /// public int Width { get { return _width; } } /// /// Initializes a new copy of the VideoContent class for the specified video file. /// /// The file name of the video to import. 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); } /// /// Immediately releases the unmanaged resources used by this object. /// 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; } } } }