-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmuscript2video
More file actions
executable file
·520 lines (459 loc) · 17.2 KB
/
muscript2video
File metadata and controls
executable file
·520 lines (459 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#! /usr/bin/perl
#
#########################################################################
# This Perl script is Copyright (c) 2013, Peter J Billam #
# c/o P J B Computing, www.pjb.com.au #
# #
# This script is free software; you can redistribute it and/or #
# modify it under the same terms as Perl itself. #
#########################################################################
my $Version = '2.1'; # extend -t and -c to allow times to be specified
my $VersionDate = '23may2018';
use File::Copy();
my $TmpDir = "/tmp/muscript2video.$$";
my $FrameNumber = 0; # used and incremented by next_filename()
my $Width = '1280'; # can be overridden with a boundingbox command
my $Height = '720'; # can be overridden with a boundingbox command
my @ThisPage = ();
my $MidiTimesig = '4/4';
my $MidiBeatsPerBar = 4; # metronome-style tempo-markings
my $TimePerBar = 2.0; # remember from page to page
my $LineNum = 0;
my %Stave2Clef = (); # replaces muscript's (undeclared?) %clef hash
my %Stave2Keysig = (); # 1.6
my $ScaleStaves = 1.46;
my $TitleJpg = '';
my $CreditsJpg = '';
my $TimeSoFar = 0.0;
my $PrePad = 0.0; my $PreTime = 3.5; # see -t 2.1
my $PostPad = 0.0; my $PostTime = 5.0; # see -c 2.1
my $PageNum = 0;
my $Fps = 5;
my $Mencoder = which('mencoder'); # 1.7
my $Ffmpeg = which('ffmpeg'); # 1.7
my $Avconv = which('avconv'); # 1.7
my $Fluadity = which('fluadity'); # 1.8
my $Timidity = which('timidity'); # 1.8
my $Reverb = 0; # 1.9 for the -r option
my $Normalize = which('normalize') || which('normalize-audio');
while ($ARGV[$[] =~ /^-([a-z])/) {
if ($1 eq 'v') { shift;
my $n = $0; $n =~ s{^.*/([^/]+)$}{$1};
print "$n version $Version $VersionDate\n";
exit 0;
} elsif ($1 eq 'c') { shift; $CreditsJpg = shift;
if ($ARGV[$[] =~ /^\d+(\.\d*)?$/) {
$PostTime = 0 + $ARGV[$[]; shift;
# print("PostTime = $PostTime\n");
}
} elsif ($1 eq 'r') { shift; $Reverb = 1;
} elsif ($1 eq 't') { shift; $TitleJpg = shift;
if ($ARGV[$[] =~ /^\d+(\.\d*)?$/) {
$PreTime = 0 + $ARGV[$[];
shift;
# print("PreTime = $PreTime\n");
}
} else {
print "usage:\n"; my $synopsis = 0;
while (<DATA>) {
if (/^=head1 SYNOPSIS/) { $synopsis = 1; next; }
if ($synopsis && /^=head1/) { last; }
if ($synopsis && /\S/) { s/^\s*/ /; print $_; next; }
}
exit 0;
}
}
if (!$Mencoder and !$Ffmpeg and !$Avconv) {
die(" Sorry, can't find mencoder, ffmpeg or avconv in your \$PATH\n");
}
if (!$Fluadity and !$Timidity) { # 1.8
die(" Sorry, can't find fluadity or timidity in your \$PATH\n");
}
if (!$Normalize) { # 2.0
die(" Sorry, can't find normalize or normalize-audio in your \$PATH\n");
}
my @Stack = <>; # slurp onto the Stack, before changing directory
if ($Stack[$[] =~ /^\s*boundingbox\s+(\d+)\s+(\d+)/) {
$Width = $1; $Height = $2;
shift @Stack;
}
if (! mkdir $TmpDir) { die "can't mkdir $TmpDir: $!\n"; }
# for the same reason, we copy TitleJpg and CreditsJpg into TmpDir
if ($TitleJpg) {
if (! File::Copy::copy($TitleJpg, "$TmpDir/pre.jpg")) {
die "can't copy $TitleJpg to $TmpDir/pre.jpg: $!\n";
}
}
if ($CreditsJpg) {
if (! File::Copy::copy($CreditsJpg, "$TmpDir/post.jpg")) {
die "can't copy $CreditsJpg to $TmpDir/post.jpg: $!\n";
}
}
if (! chdir $TmpDir) { die "can't chdir $TmpDir: $!\n"; }
# convert whole file to .mid using muscript thence to .wav using timidity
# can't pipe directly, else timidity quits too soon when its stdin closes
if (! open(P, '|-', 'muscript -midi > t.mid')) {
die "can't run muscript -midi > t.mid: $!\n";
}
print P @Stack;
close P;
wait;
if ($Fluadity) { # 1.8
system "$Fluadity t.mid tmp.wav";
} else {
system "$Timidity -Ow -o tmp.wav t.mid";
}
system "$Normalize --peak tmp.wav";
if ($TitleJpg or $CreditsJpg) {
if ($TitleJpg) {
$PrePad = $PreTime;
$TimeSoFar += $PrePad;
my $first_filename = next_filename();
rename 'pre.jpg', $first_filename;
my $num_links = round($Fps*$PrePad - 3);
foreach (1..$num_links) {
$new_filename = next_filename();
if (! link $first_filename, $new_filename) {
die "can't link $first_filename, $new_filename : $!\n";
}
}
}
if ($CreditsJpg) { $PostPad = $PostTime; }
if ($Reverb) {
warn "sox tmp.wav t.wav gain -2 pad $PrePad $PostPad reverb\n";
system "sox tmp.wav t.wav gain -2 pad $PrePad $PostPad reverb";
} else {
warn "sox tmp.wav t.wav pad $PrePad $PostPad\n";
system "sox tmp.wav t.wav pad $PrePad $PostPad";
}
unlink 'tmp.wav';
} else {
rename 'tmp.wav', 't.wav';
}
while (@Stack) { # the main loop through the input-file
$LineNum++;
my $line = shift @Stack;
while ($line =~ s{\\\n$}{}) {
my $nxt = shift @Stack; $line .= $nxt; $LineNum++;
}
# divide the file into pages
if ($line =~ /^\s*\d+\s+systems/) {
# warn(" systems line seen at line $LineNum\n");
if (@ThisPage) { page2jpgs(@ThisPage); } # get rid of previous page
@ThisPage = ($line); # start new one
} else {
push @ThisPage, $line;
}
}
if (@ThisPage) { page2jpgs(@ThisPage); }
if ($CreditsJpg) {
my $first_filename = next_filename();
rename 'post.jpg', $first_filename;
my $num_creditframes = round($Fps*$PostPad - 1);
foreach (1..$num_creditframes) {
my $new_filename = next_filename();
if (! link $first_filename, $new_filename) {
die "can't link $first_filename, $new_filename : $!\n";
}
}
}
my $aspect = sprintf '%5g', $Width/$Height;
if ($Mencoder) {
# generate the video: MPEG4 video,
# mencoder -oac help gives: Available codecs:
# copy - frame copy, without re-encoding (useful for AC3)
# pcm - uncompressed PCM audio
# mp3lame - cbr/abr/vbr MP3 using libmp3lame
# lavc - FFmpeg audio encoder (MP2, AC3, ...)
# mp3lame and faac and x264enc are not necessarily not compiled in;
# in practice, pcm works.
system "$Mencoder -audiofile t.wav -oac pcm -fps $Fps -ofps $Fps "
. "'mf://t_*.jpg' -mf h=$Height:w=$Width:fps=$Fps:type=jpeg "
. "-o t.avi -ovc lavc -lavcopts vcodec=mpeg4:aspect=$aspect";
# 20130703 and yet, it comes out about 10% too fast, then waits at
# the final frame for the audio to finish. Fixed by using -oac pcm !
# 20130705 yet now, with f3o, around bars 110-130 the video starts running
# late, ending up about 2 bars late by the end.
# It's a long-standing muscript bug !!! fixed in 3.1s !!! :-)
} elsif ($Ffmpeg) {
# ffmpeg [[infile options][-i infile]]... {[outfile options] outfile}..
# For creating a video from many images:
# ffmpeg -f image2 -i foo-%03d.jpeg -r 12 -s 1280x720 foo.avi
# "image2" is the image file demuxer,
# which reads from a list of image files specified by a pattern.
system "$Ffmpeg -i t.wav -f image2 -r $Fps -i t_%06d.jpg "
. "-r $Fps -s ${Width}x$Height t.avi"; # 1.7 worked first try :-)
} elsif ($Avconv) {
system "$Avconv -i t.wav -f image2 -r $Fps -i t_%06d.jpg "
. "-r $Fps -s ${Width}x$Height t.avi";
# 1.7 timidity causes a scrunch aftefact at start of audio
} else {
die " BUG: shouldn't reach here...";
}
system "mplayer t.avi";
warn "\n your video is: $TmpDir/t.avi\n";
# clean up ?
exit;
sub which {
my @PATH = split (":",$ENV{PATH});
my $f; foreach $d (@PATH) {$f="$d/$_[$[]"; return $f if -x $f; }
}
sub page2jpgs { my @lines = @_;
# generate the eps thence the jpg
# if it's the first time this stave appears on the page,
# regurgitate the remembered clef.
$PageNum += 1;
my $barnum_on_this_page = 0; # we are within which bar ?
foreach my $line (@lines) {
$line =~ s/^\s*(#P)?\s*//;
my @words = split ' ', $line;
if ($barnum_on_this_page > 1.5) { # note keysigs in later bars
if ($words[$[] =~ /^=(\d+)/) {
my $stave = $1;
if ($line =~ m{\s(\d+[b#])\s}) { # 1.6
$Stave2Keysig{$stave} = $1;
# warn "2) Stave2Keysig{$stave}=$Stave2Keysig{$stave}\n";
} elsif ($line =~ m{\s0\s}) { # 1.6
delete $Stave2Keysig{$stave};
# warn "2) deleting Stave2Keysig{$stave}\n";
}
}
next;
}
if ($words[$[] eq 'pagenum') {
if (! $words[$[+1]) { $line = "pagenum $PageNum\n";
} elsif ($words[$[+1] =~ /^\d+$/) { $PageNum = 0 + $words[$[+1];
} else { warn " strange line: $line\n";
}
} elsif ($words[$[] =~ /\|/) { # only process the first bar here...
$barnum_on_this_page += 1;
if ($barnum_on_this_page > 1.5) { next; }
} elsif ($words[$[] =~ /^=(\d+)/) {
my $stave = $1;
my $keysig_is_present = 0;
if ($line =~ m{\s(\d+[b#])\s}) { # 1.6
$Stave2Keysig{$stave} = $1;
$keysig_is_present = 1;
# warn "1) Stave2Keysig{$stave}=$Stave2Keysig{$stave}\n";
}
if (is_a_clef($words[$[+1])) {
if ($Stave2Keysig{$stave} and !$keysig_is_present) { # 1.6
my $keysig = $Stave2Keysig{$stave};
$line =~ s/\s(\w+)\s/ $1 $keysig /;
}
} else {
my $clef = $Stave2Clef{$stave};
if ($Stave2Keysig{$stave} and !$keysig_is_present) { # 1.6
$clef .= " $Stave2Keysig{$stave}";
}
$line =~ s/\s/ $clef /;
}
}
}
my $first_filename = next_filename();
if (! open(P, '|-', "muscript | eps2jpg > $first_filename")) {
die "can't run muscript | eps2jpg > $first_filename : $!\n";
}
print P "boundingbox $Width $Height\n", @lines;
close P;
wait;
# take a snapshot for possible diagnostic purposes
my $tf = $first_filename; $tf =~ s/jpg$/txt/; open(F, '>', $tf);
print F "boundingbox $Width $Height\n", @lines;
close F;
# and hardlink to the initial jpg the right number of times
my $time_this_page = 0.0;
my $time_this_bar = 0.0;
foreach my $line (@lines) {
$line =~ s/^\s+//;
if ($line =~ /^(\|\s*(\d+\/\d+)?\s+)(\S.*)$/) {
my $first_bit = $1; # not used here...
my $timesig = $2;
my $durations = $3;
if ($timesig and ($MidiTimesig ne $timesig)) {
$MidiTimesig = $timesig;
my ($nn,$dd) = split '/', $MidiTimesig;
if (($dd eq '8' or $dd eq '16' or $dd eq '32')
and ($nn%3 == 0)) {
$MidiBeatsPerBar = round($nn / 3);
} else {
$MidiBeatsPerBar = 0 + $nn;
}
}
my @numbers = split (' ', $durations);
$time_this_bar = 0.0;
foreach my $number (@numbers) {
if ((0+$number) > 10) { # it's a metronome-style beats/min
my $num_numbers = scalar @numbers;
$time_this_bar += (
$MidiBeatsPerBar * 60 / ($number * $num_numbers));
} else {
$time_this_bar += $number; # it's in seconds
}
}
$time_this_page += $time_this_bar;
$TimePerBar = $time_this_bar;
} elsif ($line =~ /^\|\s*(\d+\/\d+)?/) {
# if @numbers is empty but the timesig changes ?
$time_this_page += $TimePerBar;
} elsif ($line =~ /^midi pause ([.\d]+)/) {
$time_this_page += $1;
} elsif ($line =~ /^=(\d+)/) { # remember any clef change...
my $stave = $1;
foreach my $word (split ' ', $line) {
if (is_a_clef($word)) { $Stave2Clef{$stave} = $word; }
}
} elsif ($line =~ /^(\d+)\s+bars?\s/) { # scale the staves
}
}
$TimeSoFar += $time_this_page;
my $num_links = round($Fps*$TimeSoFar - $FrameNumber) - 1;
foreach (1 .. $num_links) {
my $new_filename = next_filename();
if (! link $first_filename, $new_filename) {
die "can't link $first_filename, $new_filename : $!\n";
}
}
}
sub next_filename {
$FrameNumber += 1;
my $filename = sprintf "t_%06d.jpg", $FrameNumber;
}
sub is_a_clef { my $s = $_[$[];
if ($s eq 'treble' || $s eq 'treble8va' || $s eq 'treble8vab' ||
$s eq 'alto' || $s eq 'tenor' ||
$s eq 'bass' || $s eq 'bass8va' || $s eq 'bass8vab' ) { return 1;
} else { return 0;
}
}
sub round { my $x = $_[$[];
if ($x > 0.0) { return int ($x + 0.5); }
if ($x < 0.0) { return int ($x - 0.5); }
return 0;
}
__END__
=pod
=head1 NAME
muscript2video - generates a page-turning video from a muscript file
=head1 SYNOPSIS
muscript2video file
muscript2video -t title.jpg -c credits.jpg file
muscript2video -t title.jpg 5 -c credits.jpg file # 5.0 sec title
muscript2video -t title.jpg -c credits.jpg 8.2 file # 8.2 sec credits
muscript2video -v # show the version
perldoc muscript2video # read the manual
=head1 DESCRIPTION
This Perl script takes a muscript score
and generates an I<.avi> file, ready for upload to I<youtube>.
It uses I<muscript -midi> and I<timidity>
to generate a I<.wav> file of the soundtrack.
The input file may begin with a I<boundingbox 1280 720> statement,
and should typically have say five or six staves per page
instead of say twelve (normal for A4 and US-letter).
The staveheights in the I<system> line should be
about 50% bigger than for A4, say perhaps 30 pixels instead of 19 point.
The muscript-file therefore needs to be copied
from the A4 muscript-file and edited by hand.
This script uses I<muscript> to generate a separate I<.jpg> file for each page,
repeating it with incrementing filenames for each fifth-of-a-second
that the page lasts.
Originally, it used I<mencoder> to generate the I<.avi> file,
using a mpeg4 video-codec and mp3 audio-codec that
seem to be handled well by I<youtube>.
But I<mencoder> is no longer available in I<debian jessie>,
so since version 1.7, I<mencoder>, I<ffmpeg> and I<avconv> can all be used.
The default is I<boundingbox 1280 720> which generates high-quality video,
and doesn't really need the bars-per-line formatting
to be changed from the Postscript version, though
even with a 50% increase in staveheight, extra bars can often be fitted in.
The old I<boundingbox 800 600> gives a 4:3 aspect ratio and is deprecated;
it will be padded by I<youtube> with blank on either side
and and the staves will look very small.
=head1 OPTIONS
=over 3
=item B<-t title.jpg> or B<-t title.jpg 4.0>
The file I<title.jpg> will be displayed before the beginning of the music.
It must be of B<exactly> the same geometry as specified
in your I<boundingbox 1280 720> statement, or 1280x720 by default.
If it is even slightly different, I<mencoder> will fail to make the video,
producing numerous but totally unhelpful error messages.
By default (see the first example) it is displayed for 3.5 seconds;
but the time can be specified (see the second example)
as a number of seconds, after the filename.
=item B<-c credits.jpg> or B<-c credits.jpg 7>
The file I<credits.jpg> will be displayed for seconds
after the end of the music.
It must be of B<exactly> the same geometry as specified
in your I<boundingbox 1280 720> statement, or 1280x720 by default.
If it is even slightly different, I<mencoder> will fail to make the video,
producing numerous but totally unhelpful error messages.
By default (see the first example) it is displayed for 5 seconds;
but the time can be specified (see the second example)
as a number of seconds, after the filename.
You can use I<eps2img> to generate these I<.jpg> files
directly from I<.eps> files such as:
%!PS-Adobe-3.0 EPSF-3.0
%%BoundingBox: 0 0 1280 720
%%EndComments
%%BeginProlog
/centreshow { % usage: x y font fontsize (string) centreshow
/s exch def /fontsize exch def /font exch def
gsave moveto font findfont fontsize scalefont setfont
gsave s false charpath flattenpath pathbbox grestore
exch 4 -1 roll pop pop s stringwidth pop -0.5 mul % dx/2
3 1 roll sub 0.5 mul % dy/2
rmoveto s show grestore
} bind def
%%EndProlog
640 360 /Times-BoldItalic 90 (www.pjb.com.au) centreshow
%%EOF
=back
=head1 QUIRKS
If you don't have I<fluadity> installed,
I<TiMidity> produces an audio file which begins with the first note.
Therefore, if your score begins with a rest, the pages will be turned
out of sync with the audio. Work around this by assigning very high
tempi to the initital rests, for example:
| 4/4 0.001 0.001 0.001 .32 .32 .32 .32 .32
=1 treble 4/4 4. rest 8 A 4 Bb 8. [F# 16 G#]
=head1 AUTHOR
Peter J Billam http://www.pjb.com.au/comp/contact.html
=head1 INSTALLATION
Choose some directory in your PATH (we'll use /usr/local/bin as an example).
Move the script to /usr/bin/local/muscript2video and make it executable.
For example,
cd /usr/local/bin/ # or somewhere else in your PATH
wget http://www.pjb.com.au/muscript/muscript2video
chmod 755 muscript2video
Edit the first line if necessary
to reflect where Perl is installed on your system.
=head1 CHANGES
20180523 2.1 extend -t and -c to allow times to be specified
20180522 2.0 use either normalize or normalize-audio
20180520 1.9 add -r option for some reverb
20180519 1.8 use either fluadity or timidity
20141207 1.7 if no mencoder (eg: in debian jessie) use ffmpeg or avconv
20130711 1.6 remember the keysig from page to page
20130707 1.5 uses t_*.jpg to avoid pre.jpg and post.jpg being included
20130707 1.4 first release
20130707 1.3 almost releaseable
20130704 1.2 pagenum increments; you must scale the staves by hand
20130703 1.1 -t and -c; needs pagenum++ and some scaling decisions
20130730 1.0 first working version
=head1 SEE ALSO
http://www.pjb.com.au/muscript/muscript2video
http://www.pjb.com.au/muscript/eps2img
http://www.pjb.com.au/muscript
http://www.pjb.com.au/midi/fluadity.html
http://www.pjb.com.au/comp/lua/fluidsynth.html
http://www.pjb.com.au/midi
timidity(1)
timidity.cfg(5)
mencoder(1)
ffmpeg(1)
avconv(1)
ffmpeg(1)
identify(1) # to check the exact dimensions of your title.jpg
sox(1) # if -t or -c or -r, muscript2video uses sox on the audio
perl(1)
=cut