B
B
BaranovskiyNE2014-05-28 18:45:08
ASP.NET
BaranovskiyNE, 2014-05-28 18:45:08

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);

does not work from under IIS, since it cannot create a Com object, I don’t like the method, since properties can be faked.
Method number 2
tried to use free ffmpeg wrappers, but in the end I didn’t find any that worked fine.
Method #3
Using DirectX, class Microsoft.DirectX.AudioVideoPlayback.Video. the library is 32-bit, there is a problem with unit tests in the studio, but the main thing is that it crashes from under IIS when trying to create a Video object with the error "Error in the application." :-).
using (var video = new Video(fileName))
{
    _resolution.Height.Check(video.Size.Width, "Ширина");
    _resolution.Height.Check(video.Size.Height, "Высота");
    
}

Method #3.1
Using the DirectShowLib wrapper -
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);                
            }

Unit tests work, from under IIS, when "basicVideo.get_VideoHeight" is received in hr, it gives the code "No such interface supported"
Method No. 4
Through the Media Player wrapper. The tests work fine, only the duration of the file is read from under IIS, the width and height are zero.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BaranovskiyNE, 2014-05-28
@BaranovskiyNE

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, "Высота");
            }

The problem is generally removed, but the options are still welcome ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question