-
Notifications
You must be signed in to change notification settings - Fork 2
/
scdwrite
executable file
·233 lines (179 loc) · 5.52 KB
/
scdwrite
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
#!/usr/bin/perl
###############################################################################
# Copyright (c) 2011 by bgvanbur
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
###############################################################################
# burn a sega cd from a backup
# requires all files for only one sega cd are in the PWD
# * supports bin/cue
# * supports iso/mp3
# * supports iso/wav
###############################################################################
use strict;
use warnings;
my $swap = 0;
my $prefix = '';
foreach my $arg (@ARGV) {
if ( $arg =~ /^-swap$/i ) {
$swap = 1;
} elsif ( $arg =~ /^-prefix=(.*)$/i ) {
$prefix = $1;
} else {
&Help();
}
}
###############################################################################
# configuration variables
###############################################################################
# useWav 0 (mp3) 1 (wav)
my $useWav = 1;
# useToc 0 (cue) 1 (toc)
my $useToc = 0;
my $deleteCreatedFiles = 0;
my $requireAudioFilesForIso = 0;
###############################################################################
# global variables
###############################################################################
# toc/cue file to use for burning
my $guide;
# list of large files generated by this script to delete when done
my @filesToDelete;
###############################################################################
# actual execution
###############################################################################
# check for bin/cue format
my @bins = (<${prefix}*.bin>,<${prefix}*.BIN>);
my @cues = (<${prefix}*.cue>,<${prefix}*.CUE>);
# determine format (bin/cue or iso/mp3 or iso/wav)
if ( $#bins == 0 && $#cues == 0 ) {
# bin/cue format
$guide = $cues[0];
} else {
# look for iso/mp3 or iso/wav format
# get iso name
my @isos = (<${prefix}*.iso>,<${prefix}*.ISO>);
if ( $#isos != 0 ) {
die "Only support one iso in PWD";
}
my $iso = $isos[0];
# convert mp3 files to wav files
if ( $useWav ) {
foreach my $mp3 ((<${prefix}*.mp3>,<${prefix}*.MP3>)) {
my $out = $mp3;
$out =~ s/\.mp3$/.wav/;
if ( ! -e $out ) {
#system("mpg123 --wav \"$out\" \"$mp3\"");
system("ffmpeg -i \"$mp3\" -vn -acodec pcm_s16le -ar 44100 -ac 2 -f wav \"$out\"");
if ( $deleteCreatedFiles ) {
push @filesToDelete, $out;
}
}
}
}
# collect audio (mp3/wave) file information
my @audios;
my $audioType;
if ( $useWav ) {
@audios = sort (<${prefix}*.wav>,<${prefix}*.WAV>);
$audioType = 'WAVE';
} else {
@audios = sort (<${prefix}*.mp3>,<${prefix}*.MP3>);
$audioType = 'MP3';
}
if ( $requireAudioFilesForIso && $#audios < 0 ) {
die "No audio files found\n";
}
# determine toc and cue filenames
$guide = $iso;
if ( $useToc ) {
$guide =~ s/\.iso$/.toc/;
$guide =~ s/\.ISO$/.TOC/;
} else {
$guide =~ s/\.iso$/.cue/;
$guide =~ s/\.ISO$/.CUE/;
}
# print toc/cue entry for data track
my $guideText = '';
if ( $useToc ) {
$guideText .= "CD_ROM\n";
$guideText .= "TRACK MODE1\n";
$guideText .= "DATAFILE \"$iso\"\n";
} else {
$guideText .= "FILE \"$iso\" BINARY\n";
$guideText .= " TRACK 01 MODE1/2048\n";
$guideText .= " INDEX 01 00:00:00\n";
}
# print toc/cue entries for audio tracks
# when track is 2, need to add a post-gap to track 1 and pre-gap to track 2
my $track = 2;
foreach my $audio (@audios) {
if ( $useToc ) {
if ( $track == 2 ) {
$guideText .= "ZERO 00:02:00 // post-gap\n";
$guideText .= "\n";
}
$guideText .= "TRACK AUDIO\n";
if ( $track == 2 ) {
$guideText .= "SILENCE 00:02:00 // pre-gap\n";
$guideText .= "START\n";
}
$guideText .= "FILE \"$audio\" 0\n";
$guideText .= "\n";
} else {
my $trackText = sprintf("%2.2d",$track);
if ( $track == 2 ) {
$guideText .= " POSTGAP 00:02:00\n";
}
$guideText .= "FILE \"$audio\" $audioType\n";
$guideText .= " TRACK $trackText AUDIO\n";
if ( $track == 2 ) {
$guideText .= " PREGAP 00:02:00\n";
}
$guideText .= " INDEX 01 00:00:00\n";
}
$track++;
}
# now write toc/cue file
open( GUIDE, ">$guide" ) or die "Cannot write file: $guide\n";
print GUIDE $guideText;
close GUIDE;
print $guideText;
}
# now burn the sega cd (guide already set to toc/cue accordingly to format)
my $options = '';
if ( $swap ) {
$options = ' --swap';
}
my $cmd = "cdrdao write$options --speed 1 --driver generic-mmc \"$guide\"";
# burn at 1x
print "Running: $cmd\n";
system($cmd);
# delete any generated files we made
# we keep the cue/toc since its a small file anyways
if ( $deleteCreatedFiles ) {
foreach my $file (@filesToDelete) {
system("rm \"$file\"");
}
}
sub Help {
die '
scdwrite [options]
[description]
Write a CD-R with the data for Sega CD.
[options]
-swap passes --swap to cdrdao
';
}