Answer the question
In order to leave comments, you need to log in
How to check the parameters of a video file uploaded to asp.net mvc site?
The task is to check the video uploaded by the user on the asp.net mvc site for compliance with the requirements for duration, codec, resolution.
I tried many ways, in the end, none of them worked on IIS:
Method # 1
using the Shell32 library
var shl = new Shell();
var fldr = shl.NameSpace(Path.GetDirectoryName(fileName));
var itm = fldr.ParseName(Path.GetFileName(fileName));
var res = new Dictionary<string, string>();
for (int i = 0; i < short.MaxValue; i++)
{
string header = fldr.GetDetailsOf(null, i);
if (!String.IsNullOrEmpty(header) && !res.ContainsKey(header))
{
string detailsOf = fldr.GetDetailsOf(itm, i);
if(!String.IsNullOrEmpty(detailsOf))
res.Add(header, detailsOf);
}
}
var lengthString = res.Where(i => i.Key.ToLower() == "length");
if (!lengthString.Any()) throw new ValidateVideoException("Не удалось определить длительность видео.");
var parts = lengthString.FirstOrDefault().Value.Split(new[] {':'});
var size = int.Parse(parts[0]) * 3600 + int.Parse(parts[1]) * 60 + int.Parse(parts[2]);
if (size > _minMax.Max) throw new ValidateVideoException("Доина файла {0} сек превышает ограничение {1} сек.", size, _minMax.Max);
if (size < _minMax.Min) throw new ValidateVideoException("Размер файла {0} сек ниже ограничения {1} сек.", size, _minMax.Min);
using (var video = new Video(fileName))
{
_resolution.Height.Check(video.Size.Width, "Ширина");
_resolution.Height.Check(video.Size.Height, "Высота");
}
try
{
var hr = graph.RenderFile(fileName, null);
DsError.ThrowExceptionForHR(hr);
var basicVideo = graph as IBasicVideo;
int videoHeight;
hr = basicVideo.get_VideoHeight(out videoHeight);
DsError.ThrowExceptionForHR(hr);
...проверяем...
}
finally
{
Marshal.ReleaseComObject(graph);
}
Answer the question
In order to leave comments, you need to log in
I was tormented for several days, and after asking the question I found this answer: using the taglib library
www.nuget.org/packages/taglib/.
She checks the validity of the video itself, and all the parameters I need.
using (var tagFile = TagLib.File.Create(fileName))
{
_resolution.Width.Check(tagFile.Properties.VideoWidth, "Ширина");
_resolution.Height.Check(tagFile.Properties.VideoHeight, "Высота");
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question