Filter SDK/avs2pcm
From Avisynth wiki
(Difference between revisions)
(first version - works only for SAMPLE_INT16 and mono) |
Revision as of 23:07, 22 January 2014
#include <stdio.h>
#include <Windows.h>
#include "avisynth.h"
#define MY_VERSION "Avs2PCM 0.01"
const AVS_Linkage *AVS_linkage = 0;
int __cdecl main(int argc, const char* argv[])
{
const char* infile = NULL;
const char* outfile = NULL;
FILE* out_fh;
if (!strcmp(argv[1], "-h")) {
fprintf(stderr, MY_VERSION "\n"
"Usage: avs2pcm in.avs out.pcm\n");
return 2;
} else {
infile = argv[1];
outfile = argv[2];
}
try {
char* sample_type;
typedef IScriptEnvironment* (__stdcall *DLLFUNC)(int);
IScriptEnvironment* env;
HMODULE avsdll = LoadLibrary("avisynth.dll");
if (!avsdll) {
fprintf(stderr, "failed to load avisynth.dll\n");
return 2;
}
DLLFUNC CreateEnv = (DLLFUNC)GetProcAddress(avsdll, "CreateScriptEnvironment");
if (!CreateEnv) {
fprintf(stderr, "failed to load CreateScriptEnvironment()\n");
FreeLibrary(avsdll);
return 1;
}
env = CreateEnv(AVISYNTH_INTERFACE_VERSION);
AVS_linkage = env->GetAVSLinkage();
AVSValue arg(infile);
AVSValue res = env->Invoke("Import", AVSValue(&arg, 1));
if (!res.IsClip()) {
fprintf(stderr, "Error: '%s' didn't return a video clip.\n", infile);
FreeLibrary(avsdll);
return 1;
}
PClip clip = res.AsClip();
if (clip->GetVersion() < 5) {
fprintf(stderr, "Error: too old version ('%d') of avisynth.dll loaded.\nplease install v2.60 or later.\n",
clip->GetVersion());
return 1;
}
VideoInfo vi = clip->GetVideoInfo();
if (!vi.HasAudio()) {
fprintf(stderr, "Error: '%s' video only clip.\n", infile);
FreeLibrary(avsdll);
return 1;
}
fprintf(stderr, " %s:\n", infile);
fprintf(stderr, " %d Herz,\n", vi.audio_samples_per_second);
fprintf(stderr, " %d channels,\n", vi.nchannels);
fprintf(stderr, " %I64d audio samples,\n", vi.num_audio_samples);
switch(vi.SampleType()) {
case SAMPLE_INT8 : sample_type = "8 bit";
break;
case SAMPLE_INT16 : sample_type = "16 bit";
break;
case SAMPLE_INT24 : sample_type = "24 bit";
break;
case SAMPLE_INT32 :
case SAMPLE_FLOAT : sample_type = "32 bit";
break;
default: sample_type = "unknown sample type";
break;
}
fprintf(stderr, " %s", sample_type);
out_fh = fopen(outfile, "wb");
if (!out_fh) {
fprintf(stderr, "fopen(\"%s\") failed", outfile);
FreeLibrary(avsdll);
return 1;
}
const __int64 start = 0;
const __int64 count = vi.num_audio_samples;
short* samples = new short[count];
clip->GetAudio(samples, 0, count, env);
__int64 bytes = vi.BytesFromAudioSamples(count);
fwrite(samples, bytes, 1, out_fh);
delete[] samples;
env->DeleteScriptEnvironment();
FreeLibrary(avsdll);
AVS_linkage = 0;
} catch(AvisynthError err) {
fprintf(stderr, "\nAvisynth error:\n%s\n", err.msg);
return 1;
}
fclose(out_fh);
return 0;
}