Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
I used to convert caf to mp3 using Wscript.shell and ffmpeg.exe when i was using asp.net but now i'm using a web api restful method in mvc 4, so i don't have a .aspx page i'm getting the following error:

Description:System.Web.HttpException (0x80004005): The component 'Wscript.Shell' cannot be created. Apartment threaded components can only be created on pages with an <%@ Page aspcompat=true %> page directive.
but i don't have an aspx page to add this tag...

the code i was using in aspx is:

VB
Dim wshShell As Object
wshShell = Server.CreateObject("Wscript.Shell")
cmd = "cmd.exe /k """"" & szCAF_PATH & """ -i """ & szPhysicalPath & """ " & Arguments & " """ & szEncodedPath & """"""
wshShell.Run(cmd)
Dim strCommand As String = "taskkill /F /IM cmd.exe"
wshShell.Run(strCommand, 0, True)
szDownloadLink = szDownloadLink.Replace(".caf", ".mp3")

and the code in mvc4 is :

VB
wshShell = HttpContext.Current.Server.CreateObject("Wscript.Shell")
cmd = "cmd.exe /k """"" & File.CAF_PATH & """ -i """ & File.PhysicalPath & """ " & File.Arguments & " """ & File.EncodedPath & """"""
wshShell.Run(File.cmd)
strCommand = "taskkill /F /IM cmd.exe"
wshShell.Run(strCommand, 0, True)
File.DownloadLink = File.DownloadLink.Replace(".caf", ".mp3")

does anyone know how can i fix this?
Posted
Comments
Sergey Alexandrovich Kryukov 16-Apr-14 12:33pm    
For goodness sake, why Wscript.shell?
And why "cmd.exe"?! How cmd.exe is related to running ffmpeg.exe.
Why taskkill?!!
All wrong, all. Nothing to discuss here.
—SA
Jocelyne El Khoury 17-Apr-14 3:59am    
but how can i save the file after conversion?

Please see my comments to the question. I am shocked. Here is what you can do:
C#
using System.Diagnostics;

//...

string path // path to ffmpeg.exe, use MapPath...
string commandLine // command line parameterts for ffmpeg; use string.Format("... {0} {1}", inputFileName, outputFileName) 
Process convertor = Process.Start(path, commandLine);
convertor.WaitForExit();
// at this point, you can use outputFileName

Please see: http://msdn.microsoft.com/en-us/library/vstudio/system.diagnostics.process[^].

—SA
 
Share this answer
 
Comments
Jocelyne El Khoury 17-Apr-14 3:29am    
but how can i save the file after conversion?
Sergey Alexandrovich Kryukov 17-Apr-14 12:55pm    
ffMpeg creates the file, according to its command line parameters where you can put file name; please see its documentation.
After it's done, you can use System.IO.FileMove(string, string) to move/rename the file to other location where you decide to serve it up. If you use such technique, you should complete it to the end of your response to the current HTTP request and consider this first output file (output from ffmpeg) a temporary file, so you may need to give it unique name. This is how:
http://msdn.microsoft.com/en-us/library/system.io.path.gettempfilename.aspx.

Does it answer your question?

—SA
Jocelyne El Khoury 22-Apr-14 3:24am    
i'm using : strCommand = String.Format("... {0} {1}", "'" & File.FilePath & File.SavedFilename & ".caf" & "'", "'" & File.FilePath & File.SavedFilename & ".mp3" & "'")

i tried with quotes and without quotes for the path ... but still nothing... the new file converted is not being saved ...

am i missing something here?
Sergey Alexandrovich Kryukov 22-Apr-14 8:38am    
Now you are having problems with string.Format. If you use it, you don't need '&'... Concatenation is a bad thing, because string is immutable...
—SA
I highly recommend Avdshare Audio Converter.
It can convert caf to various audio format like aiff, m4a, mp3, wav, wma, aac, etc.
It also helps to convert voc, m4a, flac, ogg, aiff, ra, au, opus etc
 
Share this answer
 
Comments
Maciej Los 3-Jul-19 14:56pm    
Wrong! This is programming forum. And your answer is not related to the question! So... This is not an answer!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900