Support widescreen mode in NHMLFixup
[jpcrr.git] / scripts / NHMLFixup.lua
blob694b46ff8ebd3649c0a17ec3e467367e241a928e
1 #!/usr/bin/env lua
2 ----------------------------------------------------------------------------------------------------------------------
3 ----------------------------------------------------------------------------------------------------------------------
4 -- NHMLFixup v9 by Ilari (2011-04-01).
5 -- Update timecodes in NHML Audio/Video track timing to conform to given MKV v2 timecodes file.
6 -- Syntax: NHMLFixup <video-nhml-file> <audio-nhml-file> <mkv-timecodes-file> [delay=<delay>] [tvaspect|widescreen]
7 -- <delay> is number of milliseconds to delay the video (in order to compensate for audio codec delay, reportedly
8 -- does not work right with some demuxers).
9 -- The 'tvaspect' option makes video track to be automatically adjusted to '4:3' aspect ratio.
10 -- The 'widescreen' option makes video track to be automatically adjusted to '16:9' aspect ratio.
12 -- Version v9 by Ilari (2011-04-01):
13 -- - Support widescreen mode ("widescreen").
15 -- Version v8 by Ilari (2010-12-06):
16 -- - Support Special timecode file "@CFR" that fixes up audio for CFR encode.
18 -- Version v7 by Ilari (2010-10-24):
19 -- - Fix bug in time division (use integer timestamps, not decimal ones).
21 -- Version v6 by Ilari (2010-10-24):
22 -- - Make it work on Lua 5.2 (work 4).
24 -- Version v5 by Ilari (2010-09-18):
25 -- - Move the files first out of way, since rename-over fails on Windows.
27 -- Version v4 by Ilari (2010-09-17):
28 -- - Change audio track ID if it collides with video track..
30 -- Version v3 by Ilari (2010-09-17):
31 -- - Support setting aspect ratio correction.
33 -- Version v2 by Ilari (2010-09-16):
34 -- - If sound and video NHMLs are wrong way around, automatically swap them.
35 -- - Check that one of the NHMLs is sound and other is video.
37 ----------------------------------------------------------------------------------------------------------------------
38 ----------------------------------------------------------------------------------------------------------------------
40 --Lua 5.2 fix:
41 unpack = unpack or table.unpack;
43 ----------------------------------------------------------------------------------------------------------------------
44 -- Function reduce_fraction(number numerator, number denumerator)
45 -- Returns reduced fraction.
46 ----------------------------------------------------------------------------------------------------------------------
47 reduce_fraction = function(numerator, denumerator)
48 local x, y = numerator, denumerator;
49 while y > 0 do
50 x, y = y, x % y;
51 end
52 return numerator / x, denumerator / x;
53 end
55 ----------------------------------------------------------------------------------------------------------------------
56 -- Function load_timecode_file(FILE file, number timescale)
57 -- Loads timecode data from file @file, using timescale of @timescale frames per second. Returns array of scaled
58 -- timecodes.
59 ----------------------------------------------------------------------------------------------------------------------
60 load_timecode_file = function(file, timescale)
61 local line, ret;
62 line = file:read("*l");
63 if line ~= "# timecode format v2" then
64 error("Timecode file is not in MKV timecodes v2 format");
65 end
66 ret = {};
67 while true do
68 line = file:read("*l");
69 if not line then
70 break;
71 end
72 local timecode = tonumber(line);
73 if not timecode then
74 error("Can't parse timecode '" .. line .. "'.");
75 end
76 table.insert(ret, math.floor(0.5 + timecode / 1000 * timescale));
77 end
78 return ret;
79 end
81 ----------------------------------------------------------------------------------------------------------------------
82 -- Function make_reverse_index_table(Array array)
83 -- Returns table, that has entry for each entry in given array @array with value being rank of value, 1 being smallest
84 -- and #array the largest. If @lookup is non-nil, values are looked up from that array.
85 ----------------------------------------------------------------------------------------------------------------------
86 make_reverse_index_table = function(array, lookup)
87 local sorted, ret;
88 local i;
89 sorted = {};
90 for i = 1,#array do
91 sorted[i] = array[i];
92 end
93 table.sort(sorted);
94 ret = {};
95 for i = 1,#sorted do
96 ret[sorted[i]] = (lookup and lookup[i]) or i;
97 end
98 return ret;
99 end
101 ----------------------------------------------------------------------------------------------------------------------
102 -- Function max_causality_violaton(Array CTS, Array DTS)
103 -- Return the maximum number of time units CTS and DTS values violate causality. #CTS must equal #DTS.
104 ----------------------------------------------------------------------------------------------------------------------
105 max_causality_violation = function(CTS, DTS)
106 local max_cv = 0;
107 local i;
108 for i = 1,#CTS do
109 max_cv = math.max(max_cv, DTS[i] - CTS[i]);
111 return max_cv;
114 ----------------------------------------------------------------------------------------------------------------------
115 -- Function fixup_video_times(Array sampledata, Array timecodes, Number spec_delay)
116 -- Fixes video timing of @sampledata (fields CTS and DTS) to be consistent with timecodes in @timecodes. Returns the
117 -- CTS offset of first sample (for fixing audio). @spec_delay is special delay to add (to fix A/V sync).
118 ----------------------------------------------------------------------------------------------------------------------
119 fixup_video_times = function(sampledata, timecodes, spec_delay)
120 local cts_tab = {};
121 local dts_tab = {};
122 local k, v, i;
124 if not timecodes then
125 local min_cts = 999999999999999999999;
126 for i = 1,#sampledata do
127 --Maximum causality violation is always zero in valid HHML.
128 sampledata[i].CTS = sampledata[i].CTS + spec_delay;
129 --Spec_delay should not apply to audio.
130 min_cts = math.min(min_cts, sampledata[i].CTS - spec_delay);
132 return min_cts;
135 if #sampledata ~= #timecodes then
136 error("Number of samples (" .. #sampledata .. ") does not match number of timecodes (" .. #timecodes
137 .. ").");
139 for i = 1,#sampledata do
140 cts_tab[i] = sampledata[i].CTS;
141 dts_tab[i] = sampledata[i].DTS;
143 cts_lookup = make_reverse_index_table(cts_tab, timecodes);
144 dts_lookup = make_reverse_index_table(dts_tab, timecodes);
146 -- Perform time translation and find max causality violation.
147 local max_cv = 0;
148 for i = 1,#sampledata do
149 sampledata[i].CTS = cts_lookup[sampledata[i].CTS];
150 sampledata[i].DTS = dts_lookup[sampledata[i].DTS];
151 max_cv = math.max(max_cv, sampledata[i].DTS - sampledata[i].CTS);
153 -- Add maximum causality violation to CTS to eliminate the causality violations.
154 -- Also find the minimum CTS.
155 local min_cts = 999999999999999999999;
156 for i = 1,#sampledata do
157 sampledata[i].CTS = sampledata[i].CTS + max_cv + spec_delay;
158 --Spec_delay should not apply to audio.
159 min_cts = math.min(min_cts, sampledata[i].CTS - spec_delay);
161 return min_cts;
164 ----------------------------------------------------------------------------------------------------------------------
165 -- Function fixup_video_times(Array sampledata, Number min_video_cts, Number video_timescale, Number audio_timescale)
166 -- Fixes video timing of @sampledata (field CTS) to be consistent with video minimum CTS of @cts. Video timescale
167 -- is assumed to be @video_timescale and audio timescale @audio_timescale.
168 ----------------------------------------------------------------------------------------------------------------------
169 fixup_audio_times = function(sampledata, min_video_cts, video_timescale, audio_timescale)
170 local fixup = math.floor(0.5 + min_video_cts * audio_timescale / video_timescale);
171 for i = 1,#sampledata do
172 sampledata[i].CTS = sampledata[i].CTS + fixup;
176 ----------------------------------------------------------------------------------------------------------------------
177 -- Function translate_NHML_TS_in(Array sampledata);
178 -- Translate NHML CTSOffset fields in @sampledata into CTS fields.
179 ----------------------------------------------------------------------------------------------------------------------
180 translate_NHML_TS_in = function(sampledata, default_dDTS)
181 local i;
182 local dts = 0;
183 for i = 1,#sampledata do
184 if not sampledata[i].DTS then
185 sampledata[i].DTS = dts + default_dDTS;
187 dts = sampledata[i].DTS;
188 if sampledata[i].CTSOffset then
189 sampledata[i].CTS = sampledata[i].CTSOffset + sampledata[i].DTS;
190 else
191 sampledata[i].CTS = sampledata[i].DTS;
196 ----------------------------------------------------------------------------------------------------------------------
197 -- Function translate_NHML_TS_out(Array sampledata);
198 -- Translate CTS fields in @sampledata into NHML CTSOffset fields.
199 ----------------------------------------------------------------------------------------------------------------------
200 translate_NHML_TS_out = function(sampledata)
201 local i;
202 for i = 1,#sampledata do
203 sampledata[i].CTSOffset = sampledata[i].CTS - sampledata[i].DTS;
204 if sampledata[i].CTSOffset < 0 then
205 error("INTERNAL ERROR: translate_NHML_TS_out: Causality violation: CTS=" .. tostring(
206 sampledata[i].CTS) .. " DTS=" .. tostring(sampledata[i].DTS) .. ".");
208 sampledata[i].CTS = nil;
212 ----------------------------------------------------------------------------------------------------------------------
213 -- Function map_table_to_number(Table tab);
214 -- Translate all numeric fields in table @tab into numbers.
215 ----------------------------------------------------------------------------------------------------------------------
216 map_table_to_number = function(tab)
217 local k, v;
218 for k, v in pairs(tab) do
219 local n = tonumber(v);
220 if n then
221 tab[k] = n;
226 ----------------------------------------------------------------------------------------------------------------------
227 -- Function map_fields_to_number(Array sampledata);
228 -- Translate all numeric fields in array @sampledata into numbers.
229 ----------------------------------------------------------------------------------------------------------------------
230 map_fields_to_number = function(sampledata)
231 local i;
232 for i = 1,#sampledata do
233 map_table_to_number(sampledata[i]);
237 ----------------------------------------------------------------------------------------------------------------------
238 -- Function escape_xml_text(String str)
239 -- Return XML escaping of text str.
240 ----------------------------------------------------------------------------------------------------------------------
241 escape_xml_text = function(str)
242 str = string.gsub(str, "&", "&amp;");
243 str = string.gsub(str, "<", "&lt;");
244 str = string.gsub(str, ">", "&gt;");
245 str = string.gsub(str, "\"", "&quot;");
246 str = string.gsub(str, "\'", "&apos;");
247 return str;
250 ----------------------------------------------------------------------------------------------------------------------
251 -- Function escape_xml_text(String str)
252 -- Return XML unescaping of text str.
253 ----------------------------------------------------------------------------------------------------------------------
254 unescape_xml_text = function(str)
255 str = string.gsub(str, "&apos;", "\'");
256 str = string.gsub(str, "&quot;", "\"");
257 str = string.gsub(str, "&gt;", ">");
258 str = string.gsub(str, "&lt;", "<");
259 str = string.gsub(str, "&amp;", "&");
260 return str;
263 ----------------------------------------------------------------------------------------------------------------------
264 -- Function serialize_table_to_xml_entity(File file, String tag, Table data, bool noclose);
265 -- Write @data as XML start tag of type @tag into @file. If noclose is true, then tag will not be closed.
266 ----------------------------------------------------------------------------------------------------------------------
267 serialize_table_to_xml_entity = function(file, tag, data, noclose)
268 local k, v;
269 file:write("<" .. tag .. " ");
270 for k, v in pairs(data) do
271 file:write(k .. "=\"" .. escape_xml_text(tostring(v)) .. "\" ");
273 if noclose then
274 file:write(">\n");
275 else
276 file:write("/>\n");
280 ----------------------------------------------------------------------------------------------------------------------
281 -- Function serialize_array_to_xml_entity(File file, String tag, Array data);
282 -- Write each element of @data as empty XML tag of type @tag into @file.
283 ----------------------------------------------------------------------------------------------------------------------
284 serialize_array_to_xml_entity = function(file, tag, data)
285 local i;
286 for i = 1,#data do
287 serialize_table_to_xml_entity(file, tag, data[i]);
291 ----------------------------------------------------------------------------------------------------------------------
292 -- Function write_NHML_data(File file, Table header, Table sampledata)
293 -- Write entiere NHML file.
294 ----------------------------------------------------------------------------------------------------------------------
295 write_NHML_data = function(file, header, sampledata)
296 file:write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n");
297 serialize_table_to_xml_entity(file, "NHNTStream", header, true);
298 serialize_array_to_xml_entity(file, "NHNTSample", sampledata);
299 file:write("</NHNTStream>\n");
302 ----------------------------------------------------------------------------------------------------------------------
303 -- Function open_file_checked(String file, String mode, bool for_write)
304 -- Return file handle to file (checking that open succeeds).
305 ----------------------------------------------------------------------------------------------------------------------
306 open_file_checked = function(file, mode, for_write)
307 local a, b;
308 a, b = io.open(file, mode);
309 if not a then
310 error("Can't open '" .. file .. "': " .. b);
312 return a;
315 ----------------------------------------------------------------------------------------------------------------------
316 -- Function call_with_file(fun, string file, String mode, ...);
317 -- Call fun with opened file handle to @file (in mode @mode) as first parameter.
318 ----------------------------------------------------------------------------------------------------------------------
319 call_with_file = function(fun, file, mode, ...)
320 -- FIXME: Handle nils returned from function.
321 local handle = open_file_checked(file, mode);
322 local ret = {fun(handle, ...)};
323 handle:close();
324 return unpack(ret);
327 ----------------------------------------------------------------------------------------------------------------------
328 -- Function xml_parse_tag(String line);
329 -- Returns the xml tag type for @line plus table of attributes.
330 ----------------------------------------------------------------------------------------------------------------------
331 xml_parse_tag = function(line)
332 -- More regexping...
333 local tagname;
334 local attr = {};
335 tagname = string.match(line, "<(%S+).*>");
336 if not tagname then
337 error("'" .. line .. "': Parse error.");
339 local k, v;
340 for k, v in string.gmatch(line, "([^ =]+)=\"([^\"]*)\"") do
341 attr[k] = unescape_xml_text(v);
343 return tagname, attr;
346 ----------------------------------------------------------------------------------------------------------------------
347 -- Function load_NHML(File file)
348 -- Loads NHML file @file. Returns header table and samples array.
349 ----------------------------------------------------------------------------------------------------------------------
350 load_NHML = function(file)
351 -- Let's regexp this shit...
352 local header = {};
353 local samples = {};
354 while true do
355 local line = file:read();
356 if not line then
357 error("Unexpected end of NHML file.");
359 local xtag, attributes;
360 xtag, attributes = xml_parse_tag(line);
361 if xtag == "NHNTStream" then
362 header = attributes;
363 elseif xtag == "NHNTSample" then
364 table.insert(samples, attributes);
365 elseif xtag == "/NHNTStream" then
366 break;
367 elseif xtag == "?xml" then
368 else
369 print("WARNING: Unrecognized tag '" .. xtag .. "'.");
372 return header, samples;
375 ----------------------------------------------------------------------------------------------------------------------
376 -- Function reame_errcheck(String old, String new)
377 -- Rename old to new. With error checking.
378 ----------------------------------------------------------------------------------------------------------------------
379 rename_errcheck = function(old, new, backup)
380 local a, b;
381 os.remove(backup);
382 a, b = os.rename(new, backup);
383 if not a then
384 error("Can't rename '" .. new .. "' -> '" .. backup .. "': " .. b);
386 a, b = os.rename(old, new);
387 if not a then
388 error("Can't rename '" .. old .. "' -> '" .. new .. "': " .. b);
392 ----------------------------------------------------------------------------------------------------------------------
393 -- Function compute_max_div(Integer ctsBound, Integer timescale, Integer maxCode, pictureOffset)
394 -- Compute maximum allowable timescale.
395 ----------------------------------------------------------------------------------------------------------------------
396 compute_max_div = function(ctsBound, timeScale, maxCode, pictureOffset)
397 -- Compute the logical number of frames.
398 local logicalFrames = ctsBound / pictureOffset;
399 local maxNumerator = math.floor(maxCode / logicalFrames);
400 -- Be conservative and assume numerator is rounded up. That is, solve the biggest maxdiv such that for all
401 -- 1 <= x <= maxdiv, maxNumerator >= ceil(x * pictureOffset / timeScale) is true.
402 -- Since maxNumerator is integer, this is equivalent to:
403 -- maxNumerator >= x * pictureOffset / timeScale
404 -- => maxNumerator * timeScale / pictureOffset >= x, thus
405 -- maxDiv = math.floor(maxNumerator * timeScale / pictureOffset);
406 return math.floor(maxNumerator * timeScale / pictureOffset);
409 ----------------------------------------------------------------------------------------------------------------------
410 -- Function rational_approximate(Integer origNum, Integer origDenum, Integer maxDenum)
411 -- Approximate origNum / origDenum using rational with maximum denumerator of maxDenum
412 ----------------------------------------------------------------------------------------------------------------------
413 rational_approximate = function(origNum, origDenum, maxDenum)
414 -- FIXME: Better approximations are possible.
415 local div = math.ceil(origDenum / maxDenum);
416 return math.floor(0.5 + origNum / div), math.floor(0.5 + origDenum / div);
419 ----------------------------------------------------------------------------------------------------------------------
420 -- Function fixup_mp4box_bug_cfr(Table header, Table samples, Integer pictureOffset, Integer maxdiv)
421 -- Fix MP4Box timecode bug for CFR video by approximating the framerate a bit.
422 ----------------------------------------------------------------------------------------------------------------------
423 fixup_mp4box_bug_cfr = function(header, samples, pictureOffset, maxdiv)
424 local oNum, oDenum;
425 local nNum, nDenum;
426 local i;
427 oNum, oDenum = pictureOffset, header.timeScale;
428 nNum, nDenum = rational_approximate(oNum, oDenum, maxdiv);
429 header.timeScale = nDenum;
430 for i = 1, #samples do
431 samples[i].DTS = math.floor(0.5 + samples[i].DTS / oNum * nNum);
432 samples[i].CTS = math.floor(0.5 + samples[i].CTS / oNum * nNum);
437 if #arg < 3 then
438 error("Syntax: NHMLFixup.lua <video.nhml> <audio.nhml> <timecodes.txt> [delay=<delay>] [tvaspect|widescreen]");
441 -- Load the NHML files.
442 io.stdout:write("Loading '" .. arg[1] .. "'..."); io.stdout:flush();
443 video_header, video_samples = call_with_file(load_NHML, arg[1], "r");
444 io.stdout:write("Done.\n");
445 io.stdout:write("Loading '" .. arg[2] .. "'..."); io.stdout:flush();
446 audio_header, audio_samples = call_with_file(load_NHML, arg[2], "r");
447 io.stdout:write("Done.\n");
448 io.stdout:write("String to number conversion on video header..."); io.stdout:flush();
449 map_table_to_number(video_header);
450 io.stdout:write("Done.\n");
451 io.stdout:write("String to number conversion on video samples..."); io.stdout:flush();
452 map_fields_to_number(video_samples);
453 io.stdout:write("Done.\n");
454 io.stdout:write("String to number conversion on audio header..."); io.stdout:flush();
455 map_table_to_number(audio_header);
456 io.stdout:write("Done.\n");
457 io.stdout:write("String to number conversion on audio samples..."); io.stdout:flush();
458 map_fields_to_number(audio_samples);
459 io.stdout:write("Done.\n");
460 if video_header.streamType == 4 and audio_header.streamType == 5 then
461 -- Ok.
462 elseif video_header.streamType == 5 and audio_header.streamType == 4 then
463 print("WARNING: You got audio and video wrong way around. Swapping them for you...");
464 audio_header,audio_samples,arg[2],video_header,video_samples,arg[1] =
465 video_header,video_samples,arg[1],audio_header,audio_samples,arg[2];
466 else
467 error("Expected one video track and one audio track");
470 if video_header.trackID == audio_header.trackID then
471 print("WARNING: Audio and video have the same track id. Assigning new track id to audio track...");
472 audio_header.trackID = audio_header.trackID + 1;
475 io.stdout:write("Computing CTS for video samples..."); io.stdout:flush();
476 translate_NHML_TS_in(video_samples, video_header.DTS_increment or 0);
477 io.stdout:write("Done.\n");
478 io.stdout:write("Computing CTS for audio samples..."); io.stdout:flush();
479 translate_NHML_TS_in(audio_samples, audio_header.DTS_increment or 0);
480 io.stdout:write("Done.\n");
482 -- Alter timescale if needed and load the timecode data.
483 delay = 0;
484 rdelay = 0;
485 for i = 4,#arg do
486 if arg[i] == "tvaspect" then
487 do_aspect_fixup = 1;
488 elseif arg[i] == "widescreen" then
489 do_aspect_fixup = 2;
490 elseif string.sub(arg[i], 1, 6) == "delay=" then
491 local n = tonumber(string.sub(arg[i], 7, #(arg[i])));
492 if not n then
493 error("Bad delay.");
495 rdelay = n;
496 delay = math.floor(0.5 + rdelay / 1000 * video_header.timeScale);
501 MAX_MP4BOX_TIMECODE = 0x7FFFFFF;
502 if arg[3] ~= "@CFR" then
503 timecode_data = call_with_file(load_timecode_file, arg[3], "r", video_header.timeScale);
504 if timecode_data[#timecode_data] > MAX_MP4BOX_TIMECODE then
505 -- Workaround MP4Box bug.
506 divider = math.ceil(timecode_data[#timecode_data] / MAX_MP4BOX_TIMECODE);
507 print("Notice: Dividing timecodes by " .. divider .. " to workaround MP4Box timecode bug.");
508 io.stdout:write("Performing division..."); io.stdout:flush();
509 video_header.timeScale = math.floor(0.5 + video_header.timeScale / divider);
510 for i = 1,#timecode_data do
511 timecode_data[i] = math.floor(0.5 + timecode_data[i] / divider);
513 --Recompute delay.
514 delay = math.floor(0.5 + rdelay / 1000 * video_header.timeScale);
515 io.stdout:write("Done.\n");
517 else
518 timecode_data = nil;
519 local maxCTS = 0;
520 local i;
521 local DTSOffset = (video_samples[2] or video_samples[1]).DTS - video_samples[1].DTS;
522 if DTSOffset == 0 then
523 DTSOffset = 1;
525 for i = 1,#video_samples do
526 if video_samples[i].CTS > maxCTS then
527 maxCTS = video_samples[i].CTS;
529 if video_samples[i].DTS % DTSOffset ~= 0 then
530 error("Video is not CFR");
532 if (video_samples[i].CTS - video_samples[1].CTS) % DTSOffset ~= 0 then
533 error("Video is not CFR");
536 if video_samples[#video_samples].CTS > MAX_MP4BOX_TIMECODE then
537 --Workaround MP4Box bug.
538 local maxdiv = compute_max_div(maxCTS, video_header.timeScale, MAX_MP4BOX_TIMECODE, DTSOffset);
539 print("Notice: Restricting denumerator to " .. maxdiv .. " to workaround MP4Box timecode bug.");
540 io.stdout:write("Fixing timecodes..."); io.stdout:flush();
541 fixup_mp4box_bug_cfr(video_header, video_samples, DTSOffset, maxdiv);
542 --Recompute delay.
543 delay = math.floor(0.5 + rdelay / 1000 * video_header.timeScale);
544 io.stdout:write("Done.\n");
548 -- Do the actual fixup.
549 io.stdout:write("Fixing up video timecodes..."); io.stdout:flush();
550 audio_fixup = fixup_video_times(video_samples, timecode_data, delay);
551 io.stdout:write("Done.\n");
552 io.stdout:write("Fixing up audio timecodes..."); io.stdout:flush();
553 fixup_audio_times(audio_samples, audio_fixup, video_header.timeScale, audio_header.timeScale);
554 io.stdout:write("Done.\n");
556 if do_aspect_fixup == 1 then
557 video_header.parNum, video_header.parDen = reduce_fraction(4 * video_header.height, 3 * video_header.width);
559 if do_aspect_fixup == 2 then
560 video_header.parNum, video_header.parDen = reduce_fraction(16 * video_header.height, 9 * video_header.width);
563 -- Save the NHML files.
564 io.stdout:write("Computing CTSOffset for video samples..."); io.stdout:flush();
565 translate_NHML_TS_out(video_samples);
566 io.stdout:write("Done.\n");
567 io.stdout:write("Computing CTSOffset for audio samples..."); io.stdout:flush();
568 translate_NHML_TS_out(audio_samples);
569 io.stdout:write("Done.\n");
570 io.stdout:write("Saving '" .. arg[1] .. ".tmp'..."); io.stdout:flush();
571 call_with_file(write_NHML_data, arg[1] .. ".tmp", "w", video_header, video_samples);
572 io.stdout:write("Done.\n");
573 io.stdout:write("Saving '" .. arg[2] .. ".tmp'..."); io.stdout:flush();
574 call_with_file(write_NHML_data, arg[2] .. ".tmp", "w", audio_header, audio_samples);
575 io.stdout:write("Done.\n");
576 io.stdout:write("Renaming '" .. arg[1] .. ".tmp' -> '" .. arg[1] .. "'..."); io.stdout:flush();
577 rename_errcheck(arg[1] .. ".tmp", arg[1], arg[1] .. ".bak");
578 io.stdout:write("Done.\n");
579 io.stdout:write("Renaming '" .. arg[2] .. ".tmp' -> '" .. arg[2] .. "'..."); io.stdout:flush();
580 rename_errcheck(arg[2] .. ".tmp", arg[2], arg[2] .. ".bak");
581 io.stdout:write("Done.\n");
582 io.stdout:write("All done.\n");