By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,905 Members | 879 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,905 IT Pros & Developers. It's quick & easy.

C# WEB:Trouble converting videos on virtual directories using Windows Media Encoder 9

P: 1
We have an ASP.NET web application (C#) that copies videos from a CCTV truck to a Linux server. What I am trying to do is convert the .AVI videos(which is how they are created on the truck) to .WMV in my C# code using Windows Media Encoder. I have a virtual directory to the truck location of the videos. I also have a virtual directory created to the Linux box. The application resides on a Windows Server 2003 and I am using VS 2005, .NET framework 2.0. The web app uses impersonation and the virtual directories are set up to connect with the impersonated user. When I use the application to copy the videos without converting, it works perfectly. When I try to implement the conversion code I receive the following error:

Attempted to read or write protected memory. This is often an indication that other memory has been corrupted

The line that fails is: m_Encoder.PrepareToEncode(true);

I have been Googling this error for days now and have found few solutions. I have tried some of the solutions I did manage to find, but nothing has helped. I'm thinking it's a permissions issue, but I can't figure out what! Any help would be greatly appreciated and please let me know if you need any more information. I am totally new to WME so I could even be doing this all wrong!
Thanks in Advance,
Carmen

Here is the code I'm using for the conversion and then the method where I am calling the ConvertFile function:

Expand|Select|Wrap|Line Numbers
  1. private static string ConvertFile(string mediaFile)
  2.         {            
  3.             UtilDaoOracle oraDao = new UtilDaoOracle();
  4.             oraDao.LogError(mediaFile, "ConvertFile", 500321);
  5.  
  6.             WMEncoder m_Encoder = new WMEncoder();
  7.  
  8.             IWMEncSourceGroup SrcGrp = (IWMEncSourceGroup)m_Encoder.SourceGroupCollection.Add("SG_1");
  9.             IWMEncVideoSource SrcVid = (IWMEncVideoSource)SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_VIDEO);
  10.  
  11.  
  12.             SrcVid.SetInput(mediaFile, "", "");
  13.  
  14.             const string ProfileName = "Screen Video Medium (CBR)";
  15.  
  16.             foreach (IWMEncProfile aProfile in m_Encoder.ProfileCollection)
  17.             {
  18.                 if (aProfile.Name == ProfileName)
  19.                 {
  20.                     SrcGrp.set_Profile(aProfile);
  21.                     oraDao.LogError(aProfile.Name.ToString(), "ConvertFileLoop", 500321);
  22.                     break;
  23.                 }
  24.             }
  25.  
  26.  
  27.             m_Encoder.File.LocalFileName = Regex.Replace(mediaFile, ".avi", ".wmv");
  28.             oraDao.LogError(m_Encoder.File.LocalFileName, "ConvertFileREturn", 500321);
  29.  
  30.             m_Encoder.AutoStop = true;
  31.  
  32.             try
  33.             {
  34.                 m_Encoder.PrepareToEncode(true);
  35.             }
  36.             catch (Exception exp)
  37.             {
  38.                 oraDao.LogError(exp.Message, "Preparetoencode", 500321);
  39.                 m_Encoder.Flush();
  40.             }
  41.  
  42.             try
  43.             {
  44.                 m_Encoder.Start();
  45.             }
  46.             catch (Exception exp)
  47.             {
  48.                 oraDao.LogError(exp.Message, "encoderstart", 500321);
  49.                 m_Encoder.Flush();
  50.             }
  51.  
  52.             return m_Encoder.File.LocalFileName;
  53.  
  54.         }
  55.  
  56.  
  57. private static bool CopyFile(string srcDir, string destDir, string fileName, bool ignoreIfInParentDir)
  58.         {
  59.  
  60.             UtilDaoOracle oraDao = new UtilDaoOracle();
  61.             FileInfo destFile = new FileInfo(CombinePath(destDir, fileName));
  62.  
  63.             //Only copy if the file does not already exist.
  64.             if (!destFile.Exists)
  65.             {
  66.                 if (ignoreIfInParentDir && File.Exists(CombinePath(destFile.Directory.Parent.FullName, fileName)))
  67.                 {
  68.                     // Return if the files already exists in the parent directory.
  69.                     return false;
  70.                 }
  71.  
  72.                 FileInfo srcFile = new FileInfo(CombinePath(srcDir, fileName));
  73.                 if (srcFile.Exists)
  74.                 {
  75.                     // Create the destination directory if not there already.
  76.                     if (!destFile.Directory.Exists)
  77.                     {
  78.                         destFile.Directory.Create();
  79.                     }
  80.  
  81.                     if (srcFile.Extension == ".avi")
  82.                     {
  83.                         string convertedFile = ConvertFile(srcFile.ToString());
  84.                         FileInfo convertedFileDone = new FileInfo(convertedFile);
  85.                         convertedFileDone.CopyTo(destFile.FullName);                        
  86.                     }
  87.                     else
  88.                     {
  89.                         srcFile.CopyTo(destFile.FullName);
  90.                     }
  91.  
  92.                     return true;
  93.                 }
  94.                 else
  95.                 {
  96.                     Logger.LogWarning(string.Format("Source file not found on machine: {0}", srcFile.FullName));
  97.                 }
  98.             }
  99.             return false;
  100.         }
  101.  
Feb 29 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.