- Home /
Running command line action through C# script
For a project I need to load in video files from the computer. However, as Unity does not support any other format besides "ogg" (right?), I will have to convert the videos from .mp4 to .ogg first. To do this I found ffmpeg that I can simply call from the command line and then it converts the video nicely.
However I would now like to automate this process by running that command line script dynamically from the Unity game itself, so the user won't have to perform any extra actions.
The script I tried to use for this looks like this:
public static void ExecuteCommand (string inputVideo)
{
var processInfo = new ProcessStartInfo("cmd.exe", @"ffmpeg -i " + inputVideo + @" -acodec libvorbis -vcodec libtheora -f ogg " + inputVideo.Split('.')[0] + @".ogg");
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
var process = Process.Start(processInfo);
process.WaitForExit();
process.Close();
}
The problem I have is that it freezes Unity when I run it through this code:
if (dialog.FileName.EndsWith(".mp4"))
{
ConvertVideo.ExecuteCommand(dialog.FileName);
}
url = dialog.FileName.Split('.')[0] + ".ogg";
Any idea what might fix my problem?
Edit: Platform is Windows in case that could make a difference.
windows platform will make a HUGE difference. windows commands cant run on linux or mac. they all have different commands.
Answer by hexagonius · Jan 16, 2016 at 04:38 PM
I think that's because the conversion takes place on the same Thread as Unity's. For this to be executed parallel to Unity you need to put the task on a seperate thread.
Maybe this is a start:
threading
This seems to fix the freezing:
public static void ExecuteCommand (string inputVideo)
{
var thread = new Thread(delegate () {Command(inputVideo);});
thread.Start();
}
static void Command (string input)
{
var processInfo = new ProcessStartInfo("cmd.exe", @"ffmpeg -i " + input + @" -acodec libvorbis -vcodec libtheora -f ogg " + input.Split('.')[0] + @".ogg");
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
var process = Process.Start(processInfo);
process.WaitForExit();
process.Close();
}
But it still doesn't give me the same result as when I run the line directly in the command prompt (cmd.exe).
Any idea what could be the cause of that?
Edit: I've directly used ffmpeg(.exe) now and it works like a charm :-) .
Hello,
I used some of this code in my own project, even calling the ffmpeg.exe directly. However, the video does not get saved.
var processInfo = new ProcessStartInfo(path, @"-i input output.wav");
path is the path to the ffmpeg... Any help here would be awesome. Thanks
PS I have my FF$$anonymous$$PEG folder in my Strea$$anonymous$$gAssets Folder
If that makes a difference.
Answer by Joshtradamus · Jan 05, 2018 at 08:35 PM
what if I want the command prompt to run a certain input when I use it. Ideally I want to push a button that will enable command prompt to pull up as a background process with a pre-input text that it responds to and continue to run in the background. How to do that all at a click of a button with c script?
'How to do that all at a click of a button with c script?'
Use menu items for example: https://docs.unity3d.com/ScriptReference/MenuItem.html
'what if I want the command prompt to run a certain input when I use it' What do you mean by input? If you want to execute a specific method, '-executeMethod' argument may be want you want: https://docs.unity3d.com/Manual/CommandLineArguments.html
Answer by talespinvr · May 27 at 01:39 PM
Is there a way to do this during play mode? My thread keeps getting aborted, likely by Unity.
Answer by Nancy963 · May 27 at 04:21 AM
The Command window is used to execute commands or aliases directly in the Visual Studio integrated development environment (IDE). You can execute both menu commands and commands that do not appear on any menu. To display the Command window, choose Other Windows from the View menu, and select Command Window. www.myfortiva.com
Your answer
![](https://koobas.hobune.stream/wayback/20220612060205im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Making a bubble level (not a game but work tool) 1 Answer
An OS design issue: File types associated with their appropriate programs 1 Answer
Get notification from command line or other application when a printer finish printng? 1 Answer