Community Forums Archive

Go Back

Subject:Insert Audio in AVI Files
Posted by: DigiMusicDoc
Date:3/30/2005 5:54:10 PM

Don't know whether this can be done or not, but if it can I would pay somebody to write a script to do it:

Locate all of the avi files in a specified directory. For each avi file insert the audio from a wav file with the same name. The avi file may or may not have existing audio.

If you're interested in doing this email me: info@digitalmusicdoctor.com.

Subject:RE: Insert Audio in AVI Files
Reply by: _TJ
Date:3/30/2005 7:57:53 PM

try this.

WARNING: THIS SCRIPT OVERWRITES YOUR ORIGINAL AVI FILES WITHOUT ASKING FIRST!!

Open the script editor (it's on the View menu).

paste the text below into the editor window.

click the Save button on the Script Editor window and save it as MergeWavIntoAvi.cs (make sure you use .cs as the file extension).

click the Run button on the Script Editor window.

the script will then run (it taks a few seconds the first time) and ask you to choose a folder.
then it will walk the folder and for each .avi that has a .wav file with the same name, it will
merge the audio data from the .wav file into the .avi file and then save the .avi file.

to save the avi files under different names. you can change the code below to use app.SaveAs() rather than app.Save().

tj

------------------------------------ code ---------------------------

using System;
using System.IO;
using System.Windows.Forms;
using SoundForge;

public class EntryPoint {
public void Begin(IScriptableApp app) {

string szPath = PromptForPath(app.Win32Window, @"c:\");
string szFilter = "*.avi";

foreach (string szAviFile in Directory.GetFiles(szPath, szFilter))
{
string szWavFile = Path.GetFileNameWithoutExtension(szAviFile);
szWavFile = Path.Combine(Path.GetDirectoryName(szAviFile),szWavFile) + ".wav";
if ( ! File.Exists(szWavFile))
{
DPF("{0} does not exist, it will be skipped.", szWavFile);
continue;
}

DPF("Merging {0} and {1}", szAviFile, szWavFile);
SfStatus result = ProcessFile(app, szAviFile, szWavFile);

DPF("~ - {0}", result);
if (result != SfStatus.Success)
break;
}
} // Begin

public SfStatus ProcessFile(IScriptableApp app, string szAviFile, string szWavFile)
{
SfStatus result = SfStatus.Success;

try
{
ISfFileHost fileWav = app.OpenFile(szWavFile, true, false);
if (null != fileWav)
{
ISfFileHost fileAvi = app.OpenFile(szAviFile, false, false);
if (null != fileAvi)
{
SfAudioSelection aselAll = new SfAudioSelection(0,-1);
fileAvi.OverwriteAudio(0, 0, fileWav, aselAll);

fileAvi.Save(SaveOptions.PromptIfNoFilename);
result = fileAvi.WaitForDoneOrCancel();

fileAvi.Close(CloseOptions.DiscardChanges);
}

fileWav.Close(CloseOptions.DiscardChanges);
}
}
catch
{
result = SfStatus.Fail;
}

return result;
}

public string PromptForPath(IWin32Window hParent, string szDir)
{
FolderBrowserDialog dlg = new FolderBrowserDialog();

dlg.Description = "Select folder for rendered files:";
if (null != szDir && "" != szDir)
dlg.SelectedPath = szDir;
else
dlg.SelectedPath = @"C:\";
dlg.ShowNewFolderButton = true;
DialogResult res = dlg.ShowDialog(hParent);
if (res == DialogResult.OK)
return dlg.SelectedPath;
return null;
}

public void FromSoundForge(IScriptableApp app) {
ForgeApp = app; //execution begins here
app.SetStatusText(String.Format("Script '{0}' is running.", Script.Name));
Begin(app);
app.SetStatusText(String.Format("Script '{0}' is done.", Script.Name));
}
public static IScriptableApp ForgeApp = null;
public static void DPF(string sz) { ForgeApp.OutputText(sz); }
public static void DPF(string fmt, object o) { ForgeApp.OutputText(String.Format(fmt,o)); }
public static void DPF(string fmt, object o, object o2) { ForgeApp.OutputText(String.Format(fmt,o,o2)); }
public static void DPF(string fmt, object o, object o2, object o3) { ForgeApp.OutputText(String.Format(fmt,o,o2,o3)); }
public static string GETARG(string k, string d) { string val = Script.Args.ValueOf(k); if (val == null || val.Length == 0) val = d; return val; }
public static int GETARG(string k, int d) { string s = Script.Args.ValueOf(k); if (s == null || s.Length == 0) return d; else return Script.Args.AsInt(k); }
public static bool GETARG(string k, bool d) { string s = Script.Args.ValueOf(k); if (s == null || s.Length == 0) return d; else return Script.Args.AsBool(k); }
} //EntryPoint

------------------------------------ code ---------------------------

Subject:RE: Insert Audio in AVI Files
Reply by: DigiMusicDoc
Date:3/31/2005 3:11:11 AM

Hey, thanks. But the first time it didn't get past the compile.

public string PromptForPath(IWin32Window hParent, string szDir)

Compiler error 0x80004005 on Line 63,29 : 'IWin32Window' is an ambiguous reference

Subject:RE: Insert Audio in AVI Files
Reply by: _TJ
Date:3/31/2005 12:04:49 PM

Oh, right. that's a bug in the script environment that I fixed for the update, but the update's not out yet. So for now, ust use null instead.

be warned though, with null it's possible for you to get the dialog to go behind Sound Forge if you click away while it is up. and once you do, you will have to move Sound Forge to the side in order to get to the dialog.

tj

Subject:RE: Insert Audio in AVI Files
Reply by: _TJ
Date:3/31/2005 12:09:17 PM

Ok, hang on. That's not gonna work either.

instead try replacing IWin32Window with SoundForge.IWin32Window

then once the update comes out, you will have to change it back.

tj

Subject:RE: Insert Audio in AVI Files
Reply by: DigiMusicDoc
Date:3/31/2005 12:46:54 PM

Got further, but still didn't quite make it.

Compiler error 0x80004005 on Line 9,22 : The best overloaded method match for 'EntryPoint.PromptForPath(SoundForge.IWin32Window, string)' has some invalid arguments

Subject:RE: Insert Audio in AVI Files
Reply by: _TJ
Date:3/31/2005 3:23:40 PM

Thats where you need to use the null

change

string szPath = PromptForPath(app.Win32Window, @"c:\");

to

string szPath = PromptForPath(null, @"c:\");


you can also change @"c:\" to some other path to make the
folder chooser dialog default to a different location.

tj

Message last edited on3/31/2005 3:24:38 PM by_TJ.
Subject:RE: Insert Audio in AVI Files
Reply by: _TJ
Date:3/31/2005 4:03:12 PM

here's the complete script with changes.
note that this script does have a bug where the folder dialog is modal to Sound Forge
but is not forced to be in front of it. But the correct code that I posted originally won't
work until you have the Sound Forge update.


using System;
using System.IO;
using System.Windows.Forms;
using SoundForge;

public class EntryPoint {
public void Begin(IScriptableApp app) {

string szPath = PromptForPath(@"c:\");
string szFilter = "*.avi";

foreach (string szAviFile in Directory.GetFiles(szPath, szFilter))
{
string szWavFile = Path.GetFileNameWithoutExtension(szAviFile);
szWavFile = Path.Combine(Path.GetDirectoryName(szAviFile),szWavFile) + ".wav";
if ( ! File.Exists(szWavFile))
{
DPF("{0} does not exist, it will be skipped.", szWavFile);
continue;
}

DPF("Merging {0} and {1}", szAviFile, szWavFile);
SfStatus result = ProcessFile(app, szAviFile, szWavFile);

DPF("~ - {0}", result);
if (result != SfStatus.Success)
break;
}
} // Begin

public SfStatus ProcessFile(IScriptableApp app, string szAviFile, string szWavFile)
{
SfStatus result = SfStatus.Success;

try
{
ISfFileHost fileWav = app.OpenFile(szWavFile, true, false);
if (null != fileWav)
{
ISfFileHost fileAvi = app.OpenFile(szAviFile, false, false);
if (null != fileAvi)
{
SfAudioSelection aselAll = new SfAudioSelection(0,-1);
fileAvi.OverwriteAudio(0, 0, fileWav, aselAll);

fileAvi.Save(SaveOptions.PromptIfNoFilename);
result = fileAvi.WaitForDoneOrCancel();

fileAvi.Close(CloseOptions.DiscardChanges);
}

fileWav.Close(CloseOptions.DiscardChanges);
}
}
catch
{
result = SfStatus.Fail;
}

return result;
}

public string PromptForPath(string szDir)
{
FolderBrowserDialog dlg = new FolderBrowserDialog();

dlg.Description = "Select folder for rendered files:";
if (null != szDir && "" != szDir)
dlg.SelectedPath = szDir;
else
dlg.SelectedPath = @"C:\";
dlg.ShowNewFolderButton = true;
DialogResult res = dlg.ShowDialog();
if (res == DialogResult.OK)
return dlg.SelectedPath;
return null;
}

public void FromSoundForge(IScriptableApp app) {
ForgeApp = app; //execution begins here
app.SetStatusText(String.Format("Script '{0}' is running.", Script.Name));
Begin(app);
app.SetStatusText(String.Format("Script '{0}' is done.", Script.Name));
}
public static IScriptableApp ForgeApp = null;
public static void DPF(string sz) { ForgeApp.OutputText(sz); }
public static void DPF(string fmt, object o) { ForgeApp.OutputText(String.Format(fmt,o)); }
public static void DPF(string fmt, object o, object o2) { ForgeApp.OutputText(String.Format(fmt,o,o2)); }
public static void DPF(string fmt, object o, object o2, object o3) { ForgeApp.OutputText(String.Format(fmt,o,o2,o3)); }
public static string GETARG(string k, string d) { string val = Script.Args.ValueOf(k); if (val == null || val.Length == 0) val = d; return val; }
public static int GETARG(string k, int d) { string s = Script.Args.ValueOf(k); if (s == null || s.Length == 0) return d; else return Script.Args.AsInt(k); }
public static bool GETARG(string k, bool d) { string s = Script.Args.ValueOf(k); if (s == null || s.Length == 0) return d; else return Script.Args.AsBool(k); }
} //EntryPoint

Subject:RE: Insert Audio in AVI Files
Reply by: DigiMusicDoc
Date:3/31/2005 5:56:23 PM

Wow! Works great. You just saved me about 2 days of work. I'm getting ready to send my Sound Forge 8 video tutorial to manufacturing, and I just got the voiceover files back from my voiceover artist. I would have had to manually match up 302 wav files with 302 avi clips. Thanks again.

Clark Murray
www.digitalmusicdoctor.com

Go Back