I have been trying to get the durationstring of a piece of video content that has been uploaded to an MVC4 application using the MediaInfo.dll which I have added to the application as a reference.
I am successfully able to view the details of the file when typing the location of my file locally using the following code:
MediaFile uploadedFile = new MediaFile("C:\\Users\\jp\\Desktop\\Quarry.mp4");
string duration = uploadedFile.General.DurationString.ToString();
However when I try to use this on an uploaded file I am not getting back the information that I had expected. My controller is as follows:
[HttpPost, ValidateInput(true)]
public ActionResult NewContent(HttpPostedFileBase postedFile, string username, FormCollection form)
{
if (postedFile != null)
{
HttpPostedFileBase postedFileCopy = postedFile;
postedFileCopy.InputStream.Position = 0;
Stream stream = postedFile.InputStream;
MediaFile uploadedFile = new MediaFile(Server.MapPath(postedFile.FileName));
string duration = uploadedFile2.General.DurationString.ToString();
string[] name = form.GetValues("name");
string[] author = form.GetValues("author");
string[] description = form.GetValues("description");
TimeSpan videoDuration = TimeSpan.Parse(duration);
try
{
avm.AddContent(postedFile, stream, Convert.ToString(name[0]), Convert.ToString(author[0]), Convert.ToString(description[0]), videoDuration);
return RedirectToAction("Contents", "Admin");
}
catch (Exception ex)
{
System.Diagnostics.EventLog.WriteEntry("Application", ex.Message, System.Diagnostics.EventLogEntryType.Error);
return RedirectToAction("Unsuccessful", "Admin");
}
}
else
return RedirectToAction("NewCourse", "Admin");
}
I have tried:
MediaFile uploadedFile = new MediaFile(Server.MapPath(postedFile.FileName));
MediaFile uploadedFile = new MediaFile(postedFile.FileName);
MediaFile uploadedFile = new MediaFile(postedFile.toString());
MediaFile uploadedFile = new MediaFile(System.IO.Path.GetFullPath(postedFile.FileName);
Any ideas of how I can get MediaInfo to recognise the postedFile in the same manner it is able to read the local file. Or how I can retrieve the path of the client machine's file location.
There is a way you can parse a file using a stream instead of a file.
Look into these methods:
It took me a while to work out how to get the IntPtr for the buffer.. remember to use GCHandle and AddrOfPinnedObject
My sample is all over the place if you need any more help I'll try to get my code working again.