Log in

View Full Version : WriteFile[If](append=false) overwrites only on first call


fvisagie
8th September 2015, 17:08
Spooling through
file = "current_frame.txt"
BlankClip(length=10)
WriteFileStart(file, """"Start of file"""", append=false)
WriteFileIf(file, "true", "current_frame", append=false, flush=true)
produces
0
1
2
3
4
5
6
7
8
9

In other words, only the first call to WriteFileIf() over-wrote the existing file (created by WriteFileStart()). All subsequent calls append. WriteFile() behaves similarly.

Is there any way of getting WriteFile[If]() to overwrite the output file at each call?

Gavino
8th September 2015, 22:29
WriteFile[If](..., append=false) overwrites any existing file, but data is written for each rendered frame. Normally, that is what one wants.

If for some reason you only want to see the data for the last rendered frame, you can use:
ScriptClip("""WriteFile[If](..., append=false)""")

With this, a separate instance of WriteFile[If] is created for every frame, each one overwriting the data written by previous one.

ajp_anton
8th September 2015, 23:40
Or maybe another "if" to only output if the current frame is the last one?

Gavino
9th September 2015, 07:47
Yes, that would work.
When writing the script, you would have to take into account that the last frame rendered is not necessarily the final frame of the source clip (eg if there are following trims).
In a complex script, it may not be obvious what frame number to check for.

fvisagie
9th September 2015, 09:06
If for some reason you only want to see the data for the last rendered frame, you can use:
ScriptClip("""WriteFile[If](..., append=false)""")

With this, a separate instance of WriteFile[If] is created for every frame, each one overwriting the data written by previous one.

That should do the trick. Thanks, Gavino.

Or maybe another "if" to only output if the current frame is the last one?

I'm afraid I don't follow you here as well as Gavino did. Please elaborate on the sequence of per-frame events you foresee here? In case it makes any difference, I was looking for a way to over-write the output file at each rendered frame, which Gavino addressed above.

Gavino
10th September 2015, 08:24
What he meant was instead of repeatedly overwriting the file, you could add an extra condition so that the file was written only for the last frame.
WritefileIf(..., "... && current_frame == frameCount-1", append=false)

That works fine for simple cases, but as I pointed out, the last frame rendered is not always the final source frame and it may not be obvious what frame number to use instead of 'frameCount-1'.

fvisagie
10th September 2015, 08:54
Thank you.

raffriff42
10th September 2015, 14:45
If I am reading the source (http://avisynth2.cvs.sourceforge.net/viewvc/avisynth2/avisynth/src/filters/conditional/conditional_reader.cpp?view=markup#l600)correctly, WriteFileIf seems to append always, regardless of the script argument...(conditional_reader.cpp)

PVideoFrame __stdcall Write::GetFrame(int n, IScriptEnvironment* env) {
...
if (Write::DoEval(env)) {
Write::FileOut(env, AplusT);
}
...

Gavino
10th September 2015, 17:56
If I am reading the source (http://avisynth2.cvs.sourceforge.net/viewvc/avisynth2/avisynth/src/filters/conditional/conditional_reader.cpp?view=markup#l600)correctly, WriteFileIf seems to append always, regardless of the script argument...
That is the per-frame code, which always appends, as stated earlier.
However the initial opening of the file is done in the constructor and the mode depends on the 'append' argument:
fout = fopen(filename, append ? AplusT : WplusT); //append or purge file

raffriff42
11th September 2015, 05:44
WriteFile[If](..., append=false) overwrites any existing file, but data is written for each rendered frame. Normally, that is what one wants...OK after rereading, I see what you mean, Gavino. I have tried to clarify this in the wiki (http://avisynth.nl/index.php/WriteFile).