-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdvd_rip.c
1063 lines (875 loc) · 32.3 KB
/
dvd_rip.c
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdbool.h>
#include <getopt.h>
#ifdef __linux__
#include <linux/cdrom.h>
#include <linux/limits.h>
#include <sys/sysinfo.h>
#include "dvd_drive.h"
#else
#include <limits.h>
#endif
#if defined (__FreeBSD__)
#include <sys/sysctl.h>
#endif
#include <dvdread/dvd_reader.h>
#include <dvdread/ifo_read.h>
#include "config.h"
#include "dvd_info.h"
#include "dvd_device.h"
#include "dvd_drive.h"
#include "dvd_open.h"
#include "dvd_vts.h"
#include "dvd_vmg_ifo.h"
#include "dvd_track.h"
#include "dvd_chapter.h"
#include "dvd_cell.h"
#include "dvd_video.h"
#include "dvd_audio.h"
#include "dvd_subtitles.h"
#include "dvd_time.h"
#include "dvd_mpv.h"
#include <mpv/client.h>
/**
* _ _ _
* __| |_ ____| | _ __(_)_ __
* / _` \ \ / / _` | | '__| | '_ \
* | (_| |\ V / (_| | | | | | |_) |
* \__,_| \_/ \__,_|___|_| |_| .__/
* |_____| |_|
*
* ** the tiniest little dvd ripper you'll ever see :) **
*
* About
*
* dvd_rip is designed to be a tiny DVD ripper with very few options. The purpose being
* that there is as little overhead as possible when wanting to do a quick DVD rip, and
* has a good set of default encoding settings.
*
* Just running "dvd_rip" alone with no arguments or options will fetch the longest track
* on the DVD, select the first English audio track, and encode it to H.264 using libx264 with
* its default preset and CRF, and AAC audio.
*
* Because it is so tiny, some options it doesn't have that larger DVD ripper applications
* would are: encoding multiple audio streams, audio passthrough, subtitle support (VOBSUB and
* closed captioning), auto-cropping black bars, and custom audio and video codec options.
*
* However(!), if you want more features when ripping the DVD, you can use "dvd_copy" instead,
* and output the track to stdout. Using ffmpeg can detect the streams and encode, crop,
* copy, and so on directly. Example: dvd_copy -o - | ffprobe -i -
*
* Presets
*
* To keep things simple, dvd_rip only has three presets which fit the most commonly used
* codecs and formats when ripping DVDs: "mp4", "mkv", "webm".
*
*/
int main(int argc, char **argv) {
/**
* Parse options
*/
bool verbose = false;
bool debug = false;
char device_filename[PATH_MAX];
bool invalid_opts = false;
bool opt_track_number = false;
bool opt_chapter_number = false;
bool opt_last_chapter = false;
bool opt_filename = false;
bool opt_start = false;
bool opt_stop = false;
char start[DVD_INFO_MPV_TIME_POSITION + 1] = {'\0'};
char stop[DVD_INFO_MPV_TIME_POSITION + 1] = {'\0'};
char length[DVD_INFO_MPV_TIME_POSITION + 1] = {'\0'};
bool x264 = false;
bool x265 = false;
bool vp8 = false;
bool vp9 = false;
bool opus = false;
bool aac = false;
bool mkv = false;
bool mp4 = false;
bool webm = false;
bool detelecine = true;
bool pal_video = false;
int8_t crf = -1;
char str_crf[10];
uint16_t arg_track_number = 1;
int long_index = 0;
int opt = 0;
size_t s = 0;
unsigned long int arg_number = 0;
uint8_t arg_first_chapter = 1;
uint8_t arg_last_chapter = 99;
char *token = NULL;
char dvd_mpv_first_chapter[4] = {'\0'};
char dvd_mpv_last_chapter[4] = {'\0'};
char dvd_mpv_audio_channels[2] = {'\0'};
const char *home_dir = getenv("HOME");
char dvd_mpv_args[64];
int retval = 0;
// Video Title Set
struct dvd_vts dvd_vts[99];
struct dvd_rip dvd_rip;
dvd_rip.track = 1;
dvd_rip.first_chapter = 1;
memset(dvd_rip.filename, '\0', PATH_MAX);
memset(dvd_rip.config_dir, '\0', PATH_MAX);
memset(dvd_rip.mpv_config_dir, '\0', PATH_MAX);
memset(dvd_rip.container, '\0', sizeof(dvd_rip.container));
memset(dvd_rip.vcodec, '\0', sizeof(dvd_rip.vcodec));
memset(dvd_rip.vcodec_opts, '\0', sizeof(dvd_rip.vcodec_opts));
memset(dvd_rip.vcodec_log_level, '\0', sizeof(dvd_rip.vcodec_log_level));
memset(dvd_rip.acodec, '\0', sizeof(dvd_rip.acodec));
memset(dvd_rip.acodec_opts, '\0', sizeof(dvd_rip.acodec_opts));
memset(dvd_rip.mpv_audio_channels, '\0', sizeof(dvd_rip.mpv_audio_channels));
memset(dvd_rip.audio_lang, '\0', sizeof(dvd_rip.audio_lang));
memset(dvd_rip.audio_stream_id, '\0', sizeof(dvd_rip.audio_stream_id));
dvd_rip.encode_subtitles = false;
memset(dvd_rip.subtitles_lang, '\0', sizeof(dvd_rip.subtitles_lang));
memset(dvd_rip.subtitles_stream_id, '\0', sizeof(dvd_rip.subtitles_stream_id));
memset(dvd_rip.vf_opts, '\0', sizeof(dvd_rip.vf_opts));
memset(dvd_rip.of_opts, '\0', sizeof(dvd_rip.of_opts));
memset(dvd_rip.start, '\0', sizeof(dvd_rip.start));
memset(dvd_rip.stop, '\0', sizeof(dvd_rip.stop));
memset(length, '\0', sizeof(length));
memset(start, '\0', sizeof(start));
memset(stop, '\0', sizeof(stop));
memset(str_crf, '\0', sizeof(str_crf));
snprintf(dvd_rip.config_dir, PATH_MAX - 1, "/.config/dvd_rip");
memset(dvd_mpv_first_chapter, '\0', sizeof(dvd_mpv_first_chapter));
memset(dvd_mpv_last_chapter, '\0', sizeof(dvd_mpv_last_chapter));
memset(dvd_mpv_args, '\0', sizeof(dvd_mpv_args));
dvd_rip.video_bitrate = 2048;
dvd_rip.audio_bitrate = 256;
if(home_dir != NULL)
snprintf(dvd_rip.mpv_config_dir, PATH_MAX - 1, "%s%s", home_dir, dvd_rip.config_dir);
struct option long_options[] = {
{ "audio", required_argument, 0, 'a' },
{ "alang", required_argument, 0, 'L' },
{ "aid", required_argument, 0, 'B' },
{ "slang", required_argument, 0, 'l' },
{ "sid", required_argument, 0, 'S' },
{ "track", required_argument, 0, 't' },
{ "chapters", required_argument, 0, 'c' },
{ "start", required_argument, 0, 's' },
{ "stop", required_argument, 0, 'p' },
{ "output", required_argument, 0, 'o' },
{ "no-detelecine", no_argument, 0, 'D' },
{ "vcodec", required_argument, 0, 'v'},
{ "acodec", required_argument, 0, 'a'},
{ "crf", required_argument, 0, 'q' },
{ "help", no_argument, 0, 'h' },
{ "version", no_argument, 0, 'V' },
{ "verbose", no_argument, 0, 'x' },
{ "debug", no_argument, 0, 'z' },
{ 0, 0, 0, 0 }
};
while((opt = getopt_long(argc, argv, "a:B:c:DhL:l:o:p:q:s:S:t:T:Vv:xz", long_options, &long_index )) != -1) {
switch(opt) {
case 'a':
if(strncmp(optarg, "aac", 3) == 0) {
aac = true;
} else if(strncmp(optarg, "opus", 4) == 0) {
opus = true;
}
break;
case 'B':
strncpy(dvd_rip.audio_stream_id, optarg, 3);
break;
case 'c':
opt_chapter_number = true;
token = strtok(optarg, "-");
if(strlen(token) > 2) {
fprintf(stderr, "[dvd_rip] Chapter range must be between 1 and 99\n");
return 1;
}
arg_number = strtoul(token, NULL, 10);
if(arg_number > 99)
arg_first_chapter = 99;
else if(arg_number == 0)
arg_first_chapter = 1;
else
arg_first_chapter = (uint8_t)arg_number;
token = strtok(NULL, "-");
if(token != NULL) {
if(strlen(token) > 2) {
fprintf(stderr, "[dvd_rip] Chapter range must be between 1 and 99\n");
return 1;
}
arg_number = strtoul(token, NULL, 10);
if(arg_number > 99)
arg_last_chapter = 99;
if(arg_number == 0)
arg_last_chapter = arg_first_chapter;
else {
opt_last_chapter = true;
arg_last_chapter = (uint8_t)arg_number;
}
}
if(arg_last_chapter < arg_first_chapter) {
fprintf(stderr, "[dvd_rip] Last chapter must be a greater number than first chapter\n");
return 1;
}
if(arg_first_chapter > arg_last_chapter) {
fprintf(stderr, "[dvd_rip] First chapter must be a lower number than first chapter\n");
return 1;
}
break;
case 'D':
detelecine = false;
break;
case 'L':
strncpy(dvd_rip.audio_lang, optarg, 2);
break;
case 'l':
strncpy(dvd_rip.subtitles_lang, optarg, 2);
break;
case 'o':
opt_filename = true;
strncpy(dvd_rip.filename, optarg, PATH_MAX - 1);
if(strcmp(strrchr(optarg, '\0') - 4, ".mkv") == 0) {
mkv = true;
} else if(strcmp(strrchr(optarg, '\0') - 5, ".webm") == 0) {
webm = true;
} else if(strcmp(strrchr(optarg, '\0') - 4, ".mp4") == 0) {
mp4 = true;
}
break;
case 'p':
opt_stop = true;
strncpy(length, optarg, DVD_INFO_MPV_TIME_POSITION);
for(s = 0; s < strlen(length); s++) {
if(!isdigit(length[s]) && !(length[s] == '-' || length[s] == '+' || length[s] == ':' || length[s] == '.' || length[s] == '%')) {
fprintf(stderr, "[dvd_rip] Invalid length format, use [+|-][[hh:]mm:]ss[.ms] or <percent>%%\n");
return 1;
}
}
strncpy(stop, length, DVD_INFO_MPV_TIME_POSITION);
break;
case 'q':
arg_number = strtoul(optarg, NULL, 10);
if(arg_number > 63)
arg_number = 63;
crf = (int8_t)arg_number;
break;
case 's':
opt_start = true;
strncpy(length, optarg, DVD_INFO_MPV_TIME_POSITION);
for(s = 0; s < strlen(length); s++) {
if(!isdigit(length[s]) && !(length[s] == '-' || length[s] == '+' || length[s] == ':' || length[s] == '.' || length[s] == '%')) {
fprintf(stderr, "[dvd_rip] Invalid length format, use [+|-][[hh:]mm:]ss[.ms] or <percent>%%\n");
return 1;
}
}
strncpy(start, length, DVD_INFO_MPV_TIME_POSITION);
break;
case 'S':
strncpy(dvd_rip.subtitles_stream_id, optarg, 3);
break;
case 't':
opt_track_number = true;
arg_number = strtoul(optarg, NULL, 10);
if(arg_number > 99)
arg_track_number = 99;
else if(arg_number == 0)
arg_track_number = 1;
else
arg_track_number = (uint16_t)arg_number;
break;
case 'V':
printf("dvd_rip %s\n", PACKAGE_VERSION);
return 0;
case 'x':
verbose = true;
break;
case 'v':
if(strncmp(optarg, "x264", 4) == 0) {
x264 = true;
} else if(strncmp(optarg, "x265", 4) == 0) {
x265 = true;
} else if(strncmp(optarg, "vp8", 3) == 0) {
vp8 = true;
} else if(strncmp(optarg, "vp9", 3) == 0) {
vp9 = true;
}
break;
case 'z':
verbose = true;
debug = true;
break;
case '?':
invalid_opts = true;
case 'h':
printf("dvd_rip - a tiny DVD ripper\n");
printf("\n");
printf("Usage:\n");
printf(" dvd_rip [path] [options]\n");
printf("\n");
printf(" -o, --output <filename> Save to filename (default: dvd_track_##.mkv)\n");
printf("\n");
printf("Track selection:\n");
printf(" -t, --track <#> Encode selected track (default: longest)\n");
printf(" -c, --chapter <#>[-[#]] Encode chapter number or range (default: all)\n");
printf("\n");
printf("Time duration:\n");
printf(" -s, --start <format> Start at time length in format in [+|-][[hh:]mm:]ss[.ms] or <percent>%%\n");
printf(" -p, --stop <format> Stop at time length in format in [+|-][[hh:]mm:]ss[.ms] or <percent>%%\n");
printf("\n");
printf("Language selection:\n");
printf(" --alang <language> Select audio language, two character code (default: first audio track)\n");
printf(" --aid <#> Select audio track ID\n");
printf(" --slang <language> Select subtitles language, two character code (default: none)\n");
printf(" --sid <#> Select subtitles track ID\n");
printf("\n");
printf("Encoding options:\n");
printf("\n");
printf(" -v, --vcodec <vcodec> Video codec <x264|x265|vp8|vp9>, default: x264\n");
printf(" -a, --acodec <acodec> Audio codec (aac|opus), default: aac\n");
printf(" -q, --crf <#> Video encoder CRF (x264 and x265)\n");
printf(" -D, --no-detelecine Do not detelecine video\n");
printf("\n");
printf("Defaults:\n");
printf("\n");
printf("By default, dvd_rip will encode source to H.264 video with Opus audio in an\n");
printf("MKV container. If an output filename is given with a different extension,\n");
printf("it will use the default settings. for those instead. In each case, the default\n");
printf("presets are used as selected by the codecs as well. Note that mpv must already\n");
printf("be built with support for these codecs, or dvd_rip will quit.\n\n");
printf("See the man page for more details.\n");
printf("\n");
printf("Other:\n");
printf(" --verbose Show verbose output\n");
printf(" --debug Show debugging output\n");
printf(" --version Show version info and exit\n");
printf(" -h, --help Show this help text and exit\n");
printf("\n");
printf("DVD path can be a device name, a single file, or directory (default: %s)\n", DEFAULT_DVD_DEVICE);
printf("\n");
printf("dvd_rip reads a configuration file from ~/.config/dvd_rip/mpv.conf\n");
printf("\n");
printf("See mpv man page for syntax and dvd_rip man page for examples.\n");
if(invalid_opts)
return 1;
return 0;
// let getopt_long set the variable
case 0:
default:
break;
}
}
memset(device_filename, '\0', PATH_MAX);
if (argv[optind])
strncpy(device_filename, argv[optind], PATH_MAX - 1);
else
strncpy(device_filename, DEFAULT_DVD_DEVICE, PATH_MAX - 1);
/** Begin dvd_rip :) */
if(access(device_filename, F_OK) != 0) {
fprintf(stderr, "[dvd_rip] cannot access %s\n", device_filename);
return 1;
}
#ifdef __linux__
// Check for device drive status
retval = device_open(device_filename);
if(retval == 1)
return 1;
#endif
// begin libdvdread usage
// Open DVD device
dvd_reader_t *dvdread_dvd = NULL;
dvd_logger_cb dvdread_logger_cb = { dvd_info_logger_cb };
dvdread_dvd = DVDOpen2(NULL, &dvdread_logger_cb, device_filename);
if(!dvdread_dvd) {
fprintf(stderr, "[dvd_rip] Opening DVD %s failed\n", device_filename);
return 1;
}
// Check if DVD has an identifier, fail otherwise
char dvdread_id[DVD_DVDREAD_ID + 1];
memset(dvdread_id, '\0', sizeof(dvdread_id));
dvd_dvdread_id(dvdread_id, dvdread_dvd);
if(strlen(dvdread_id) == 0) {
fprintf(stderr, "[dvd_rip] Opening DVD %s failed\n", device_filename);
return 1;
}
ifo_handle_t *vmg_ifo = NULL;
vmg_ifo = ifoOpen(dvdread_dvd, 0);
if(vmg_ifo == NULL || !ifo_is_vmg(vmg_ifo)) {
fprintf(stderr, "[dvd_rip] Opening VMG IFO failed\n");
DVDClose(dvdread_dvd);
return 1;
}
// DVD
struct dvd_info dvd_info;
dvd_info.video_title_sets = dvd_video_title_sets(vmg_ifo);
memset(dvd_info.title, '\0', sizeof(dvd_info.title));
dvd_info.tracks = dvd_tracks(vmg_ifo);
dvd_info.longest_track = 1;
dvd_title(dvd_info.title, device_filename);
uint16_t num_ifos = 1;
num_ifos = vmg_ifo->vts_atrt->nr_of_vtss;
if(num_ifos < 1) {
fprintf(stderr, "[dvd_rip] DVD has no title IFOs\n");
ifoClose(vmg_ifo);
DVDClose(dvdread_dvd);
return 1;
}
// Cells
struct dvd_cell dvd_cell;
dvd_cell.cell = 1;
memset(dvd_cell.length, '\0', sizeof(dvd_cell.length));
snprintf(dvd_cell.length, DVD_CELL_LENGTH + 1, "00:00:00.000");
dvd_cell.msecs = 0;
// Open first IFO
uint16_t vts = 1;
ifo_handle_t *vts_ifo = NULL;
vts_ifo = ifoOpen(dvdread_dvd, vts);
if(vts_ifo == NULL) {
fprintf(stderr, "[dvd_rip] Could not open primary VTS_IFO\n");
return 1;
}
ifoClose(vts_ifo);
vts_ifo = NULL;
// Create an array of all the IFOs
ifo_handle_t *vts_ifos[DVD_MAX_VTS_IFOS];
vts_ifos[0] = NULL;
for(vts = 1; vts < dvd_info.video_title_sets + 1; vts++) {
dvd_vts[vts].vts = vts;
dvd_vts[vts].valid = false;
dvd_vts[vts].blocks = 0;
dvd_vts[vts].filesize = 0;
dvd_vts[vts].vobs = 0;
dvd_vts[vts].tracks = 0;
dvd_vts[vts].valid_tracks = 0;
dvd_vts[vts].invalid_tracks = 0;
vts_ifos[vts] = ifoOpen(dvdread_dvd, vts);
if(vts_ifos[vts] == NULL) {
dvd_vts[vts].valid = false;
vts_ifos[vts] = NULL;
} else if(!ifo_is_vts(vts_ifos[vts])) {
dvd_vts[vts].valid = false;
ifoClose(vts_ifos[vts]);
vts_ifos[vts] = NULL;
} else {
dvd_vts[vts].valid = true;
}
}
// Exit if track number requested does not exist
if(opt_track_number && (arg_track_number > dvd_info.tracks)) {
fprintf(stderr, "[dvd_rip] Invalid track number %" PRIu16 "\n", arg_track_number);
fprintf(stderr, "[dvd_rip] Valid track numbers: 1 to %" PRIu16 "\n", dvd_info.tracks);
ifoClose(vmg_ifo);
DVDClose(dvdread_dvd);
return 1;
} else if(opt_track_number) {
dvd_rip.track = arg_track_number;
}
// Set the track number to the longest if none is passed as an argument
if(!opt_track_number) {
uint16_t ix = 0;
uint16_t track = 1;
uint32_t msecs = 0;
uint32_t longest_msecs = 0;
for(ix = 0, track = 1; ix < dvd_info.tracks; ix++, track++) {
vts = dvd_vts_ifo_number(vmg_ifo, track);
vts_ifo = vts_ifos[vts];
msecs = dvd_track_msecs(vmg_ifo, vts_ifo, track);
if(msecs > longest_msecs) {
longest_msecs = msecs;
dvd_rip.track = track;
}
}
}
// Track
struct dvd_track dvd_track;
dvd_track.track = dvd_rip.track;
dvd_track.valid = true;
dvd_track.vts = dvd_vts_ifo_number(vmg_ifo, dvd_rip.track);
vts_ifo = vts_ifos[dvd_track.vts];
dvd_track.ttn = dvd_track_ttn(vmg_ifo, dvd_rip.track);
dvd_track_length(dvd_track.length, vmg_ifo, vts_ifo, dvd_rip.track);
dvd_track.msecs = dvd_track_msecs(vmg_ifo, vts_ifo, dvd_rip.track);
dvd_track.chapters = dvd_track_chapters(vmg_ifo, vts_ifo, dvd_rip.track);
dvd_track.audio_tracks = dvd_track_audio_tracks(vts_ifo);
dvd_track.subtitles = dvd_track_subtitles(vts_ifo);
dvd_track.active_audio_streams = dvd_audio_active_tracks(vmg_ifo, vts_ifo, dvd_rip.track);
dvd_track.active_subs = dvd_track_active_subtitles(vmg_ifo, vts_ifo, dvd_rip.track);
dvd_track.cells = dvd_track_cells(vmg_ifo, vts_ifo, dvd_rip.track);
dvd_track.blocks = dvd_track_blocks(vmg_ifo, vts_ifo, dvd_rip.track);
dvd_track.filesize = dvd_track_filesize(vmg_ifo, vts_ifo, dvd_rip.track);
// Calculating the number of output chapters. Loop through all the audio tracks that
// are on the title track, and simply choose what's the highest so we definitely get
// whatever the greatest source is. In some cases, this will mean upmixing to more channels.
uint8_t audio_track_ix;
uint8_t audio_max_channels = 0;
for(audio_track_ix = 0; audio_track_ix < dvd_track.audio_tracks; audio_track_ix++) {
dvd_rip.audio_channels = dvd_audio_channels(vts_ifo, audio_track_ix);
if(dvd_rip.audio_channels > audio_max_channels)
audio_max_channels = dvd_rip.audio_channels;
}
// Default last chapter to the number of chapters
dvd_rip.last_chapter = dvd_track.chapters;
// Set the proper chapter range
if(opt_chapter_number && !opt_last_chapter) {
if(arg_first_chapter > dvd_track.chapters) {
fprintf(stderr, "[dvd_rip] Starting chapter cannot be larger than %" PRIu8 "\n", dvd_track.chapters);
return 1;
}
// Only set first chapter, last is set above by default
dvd_rip.first_chapter = arg_first_chapter;
}
if(opt_chapter_number && opt_last_chapter) {
if(arg_last_chapter > dvd_track.chapters) {
fprintf(stderr, "[dvd_rip] Final chapter cannot be larger than %" PRIu8 "\n", dvd_track.chapters);
return 1;
}
dvd_rip.first_chapter = arg_first_chapter;
dvd_rip.last_chapter = arg_last_chapter;
}
/**
* File descriptors and filenames
*/
dvd_file_t *dvdread_vts_file = NULL;
vts = dvd_vts_ifo_number(vmg_ifo, dvd_rip.track);
vts_ifo = vts_ifos[vts];
// Open the VTS VOB
dvdread_vts_file = DVDOpenFile(dvdread_dvd, vts, DVD_READ_TITLE_VOBS);
if(strlen(dvd_info.title))
fprintf(stderr, "[dvd_rip] Disc title: '%s'\n", dvd_info.title);
if(strlen(dvdread_id))
fprintf(stderr, "[dvd_rip] Disc ID: '%s'\n", dvdread_id);
fprintf(stderr, "[dvd_rip] Track: %*" PRIu16 ", Length: %s, Chapters: %*" PRIu8 ", Cells: %*" PRIu8 ", Audio streams: %*" PRIu8 ", Subpictures: %*" PRIu8 "\n", 2, dvd_track.track, dvd_track.length, 2, dvd_track.chapters, 2, dvd_track.cells, 2, dvd_track.audio_tracks, 2, dvd_track.subtitles);
// Check for track issues
if(dvd_vts[vts].valid == false) {
dvd_track.valid = false;
}
if(dvd_track.msecs == 0) {
fprintf(stderr, "[dvd_rip] invalid track - track has zero length\n");
dvd_track.valid = false;
}
if(dvd_track.chapters == 0) {
fprintf(stderr, "[dvd_rip] invalid track - zero chapters\n");
dvd_track.valid = false;
}
if(dvd_track.cells == 0) {
fprintf(stderr, "[dvd_rip] invalid track - zero cells\n");
dvd_track.valid = false;
}
if(!dvd_track.valid) {
fprintf(stderr, "[dvd_rip] track %" PRIu16 "has been flagged as invalid, and encoding it could cause strange problems\b", dvd_rip.track);
}
// MPV instance
mpv_handle *dvd_mpv = NULL;
dvd_mpv = mpv_create();
if(dvd_mpv == NULL) {
fprintf(stderr, "[dvd_rip] could not create an mpv instance\n");
return 1;
}
mpv_set_option_string(dvd_mpv, "config-dir", dvd_rip.mpv_config_dir);
mpv_set_option_string(dvd_mpv, "config", "yes");
mpv_set_option_string(dvd_mpv, "terminal", "yes");
if(debug) {
mpv_request_log_messages(dvd_mpv, "debug");
strcpy(dvd_rip.vcodec_log_level, "full");
} else if (verbose) {
mpv_request_log_messages(dvd_mpv, "v");
strcpy(dvd_rip.vcodec_log_level, "info");
} else {
mpv_request_log_messages(dvd_mpv, "info");
strcpy(dvd_rip.vcodec_log_level, "info");
}
/** DVD **/
snprintf(dvd_mpv_args, sizeof(dvd_mpv_args), "dvd://%" PRIu16, dvd_rip.track - 1);
const char *dvd_mpv_commands[] = { "loadfile", dvd_mpv_args, NULL };
mpv_set_option_string(dvd_mpv, "dvd-device", device_filename);
/* Chapters */
// Starting chapter or time point
// Don't need to change the start point string (--start), just add # to chapter number string
if(opt_chapter_number && opt_start) {
fprintf(stderr, "[dvd_rip] starting position must be either chapter number or time length format\n");
return 1;
}
// Set first chapter if given
if(!opt_start && !opt_chapter_number) {
dvd_rip.first_chapter = 1;
} else if(opt_chapter_number && dvd_rip.first_chapter > 1) {
snprintf(dvd_rip.start, sizeof(dvd_rip.start), "#%" PRIu8, dvd_rip.first_chapter);
} else {
if(!strlen(start))
strncpy(start, "0", 2);
strncpy(dvd_rip.start, start, sizeof(dvd_rip.start));
fprintf(stderr, "[dvd_rip] starting position: #%s\n", dvd_rip.start);
}
mpv_set_option_string(dvd_mpv, "start", dvd_rip.start);
// Don't allow setting last chapter and stop position
if(opt_last_chapter && opt_stop) {
fprintf(stderr, "[dvd_rip] stopping position must be either chapter number or time length format\n");
return 1;
}
// To play a chapter range in MPV, the end chapter needs to be incremented to the final one plus one
// *unless* selecting just the final chapter. dvd_rip -c 1-1 would be mpv --start=#1 --end=#2
if(dvd_rip.last_chapter != dvd_track.chapters)
dvd_rip.mpv_last_chapter = dvd_rip.last_chapter + 1;
// Stop at either length given, or last chapter passed
if(!opt_stop) {
if(dvd_rip.last_chapter == dvd_track.chapters)
dvd_rip.mpv_last_chapter = dvd_rip.last_chapter;
snprintf(dvd_rip.stop, sizeof(dvd_rip.stop), "#%" PRIu8, dvd_rip.mpv_last_chapter);
mpv_set_option_string(dvd_mpv, "end", dvd_rip.stop);
fprintf(stderr, "[dvd_rip] stopping chapter: %" PRIu8 "\n", dvd_rip.last_chapter);
} else {
strncpy(dvd_rip.stop, stop, sizeof(dvd_rip.stop));
mpv_set_option_string(dvd_mpv, "length", dvd_rip.stop);
fprintf(stderr, "[dvd_rip] stopping position: %s\n", dvd_rip.stop);
}
// If you pass a stop parameter that goes past *the chapter stop position*, then it's going to do
// who knows what.
if(verbose && opt_stop && dvd_rip.last_chapter == dvd_track.chapters) {
fprintf(stderr, "[dvd_rip] WARNING: if --stop position given is past the last chapter, libmpv could do something unexpected\n");
}
// Default container MP4
if(!mp4 && !mkv && !webm)
mp4 = true;
// Default video codec for MP4 and MKV
if(!x264 && !x265 && !vp8 && !vp9)
x264 = true;
// Default video codec for WebM
if(webm && !vp8 && !vp9) {
x264 = false;
vp8 = true;
}
// MP4 can't support VP8 or VP9
if(mp4 && (vp8 || vp9)) {
fprintf(stderr, "[dvd_rip] MP4 doesn't support VP8 or VP9 video codecs, quitting\n");
return 1;
}
// WebM can't support x264 or x265
if(webm && (x264 || x265)) {
fprintf(stderr, "[dvd_rip] WebM only supports VP8 and VP9 video codecs, quitting\n");
return 1;
}
// WebM can't support AAC
if(webm && aac) {
fprintf(stderr, "[dvd_rip] WebM only supports Opus audio codec, quitting\n");
return 1;
}
if(!aac && !opus && !webm)
aac = true;
if(!aac && !opus && webm)
opus = true;
/** Filename **/
if(!opt_filename) {
if(mp4)
strcpy(dvd_rip.container, "mp4");
else if(mkv)
strcpy(dvd_rip.container, "mkv");
else if(webm)
strcpy(dvd_rip.container, "webm");
sprintf(dvd_rip.filename, "dvd_track_%02" PRIu16 ".%s", dvd_rip.track, dvd_rip.container);
}
mpv_set_option_string(dvd_mpv, "o", dvd_rip.filename);
fprintf(stderr, "[dvd_rip] saving to filename \'%s\'\n", dvd_rip.filename);
/** Encoding Notes **/
// I'm not going to output the exact mpv commands for defaults, but this is vp8 and opus (as of right now)
// mpv dvd://0 --dvd-device=1.000.PRIME_SAMPLE.iso --ovc=libvpx --ovcopts=b=2048k,cpu-used=8 --oac=libopus --oacopts=b=256k -o test.mkv
/** Video **/
// Fix input CRF if needed
if((x264 || x265) && crf > 51)
crf = 51;
if((vp8 || vp9) && crf > 63)
crf = 63;
if(x264 && crf == -1)
crf = 20;
if(x265 && crf == -1)
crf = 22;
if(crf > -1 && (x264 || x265)) {
snprintf(dvd_rip.vcodec_opts, 9, "crf=%i", crf);
}
if(vp8 || vp9) {
// Set some high quality defaults for libvpx
if(vp8)
dvd_rip.video_bitrate = 2048;
else
dvd_rip.video_bitrate = 1536;
/* Trying to set encoding parameters for VPX is a pain, because I can't
* tell if I've got the syntax wrong or not, though everything from ffmpeg
* docs looks like I'm doing it right. That being said, setting a CRF
* does not work, so I'm trying to set a relevantly decent bitrate for
* both vp8 and vp9.
*
* I would like to use qmin and qmax as my second choice, but that's not
* working either. Using ffmpeg docs as reference:
* https://trac.ffmpeg.org/wiki/Encode/VP8
* https://ffmpeg.org//ffmpeg-all.html#libvpx
*
* Here's all the things I've tested that *don't* work:
* - crf=20,b=0
* - qmin=0,qmax=50
* - qmin=0,qmax=50,crf=20
*
* Another issue is that libvpx encodes using *one* processor at a time,
* so I'm overriding it here to actual # available (see sysctl.h). It's
* obviously much faster now.
*
* So here you go, me guessing at bitrates. Hopefully in the future
* I'll figure this out, but for now, this is it.
*/
if(vp8 || vp9) {
int nprocs = 1;
#if defined (__FreeBSD__)
int mib[2] = { CTL_HW, HW_NCPU };
size_t len = sizeof(nprocs);
sysctl(mib, 2, &nprocs, &len, NULL, 0);
#else
nprocs = get_nprocs();
#endif
snprintf(dvd_rip.vcodec_opts, sizeof(dvd_rip.vcodec_opts), "b=%" PRIu16 "k,cpu-used=%i", dvd_rip.video_bitrate, nprocs);
}
}
/**
* For some reason, when passing a chapter range that does *not* start with the
* first one, the output file puts the audio stream first (and it is driving me
* crazy). mpv-0.37.0
* See: ./dvd_rip 1.000.PRIME_SAMPLE.iso -c 2-2
*/
// Video codecs and encoding options
if (x264)
strcpy(dvd_rip.vcodec, "libx264");
else if(x265)
strcpy(dvd_rip.vcodec, "libx265");
else if (vp8) {
strcpy(dvd_rip.vcodec, "libvpx");
} else if (vp9) {
strcpy(dvd_rip.vcodec, "libvpx-vp9");
}
mpv_set_option_string(dvd_mpv, "ovc", dvd_rip.vcodec);
// Containers
if(mp4) {
strcpy(dvd_rip.of_opts, "movflags=+faststart");
mpv_set_option_string(dvd_mpv, "ofopts", dvd_rip.of_opts);
}
if(strlen(dvd_rip.vcodec_opts))
mpv_set_option_string(dvd_mpv, "ovcopts", dvd_rip.vcodec_opts);
// Detelecining
if(detelecine && pal_video)
strcat(dvd_rip.vf_opts, "pullup,dejudder,fps=25");
else if(detelecine && !pal_video)
strcat(dvd_rip.vf_opts, "pullup,dejudder,fps=fps=30000/1001");
mpv_set_option_string(dvd_mpv, "vf", dvd_rip.vf_opts);
/** Audio **/
// Audio codec
if(aac)
strcpy(dvd_rip.acodec, "aac");
else if(opus)
strcpy(dvd_rip.acodec, "libopus");
// There are some DVD tracks with no audio channels, and mpv will die if number is set
if(audio_max_channels) {
if(strlen(dvd_rip.audio_lang))
mpv_set_option_string(dvd_mpv, "alang", dvd_rip.audio_lang);
else if(strlen(dvd_rip.audio_stream_id))
mpv_set_option_string(dvd_mpv, "aid", dvd_rip.audio_stream_id);
mpv_set_option_string(dvd_mpv, "oac", dvd_rip.acodec);
// Use same channel layout as source; AAC and Opus both support 5.1
snprintf(dvd_rip.mpv_audio_channels, sizeof(dvd_rip.mpv_audio_channels), "%" PRIu8, audio_max_channels);
mpv_set_option_string(dvd_mpv, "audio-channels", dvd_rip.mpv_audio_channels);
// Use a high bitrate by default to guarantee good sound quality
snprintf(dvd_rip.acodec_opts, sizeof(dvd_rip.acodec_opts), "b=%" PRIu16 "k", dvd_rip.audio_bitrate);
mpv_set_option_string(dvd_mpv, "oacopts", dvd_rip.acodec_opts);
}
/** Subtitles **/
if(strlen(dvd_rip.subtitles_lang)) {
fprintf(stderr, "[dvd_rip] burn-in subtitles lang '%s'\n", dvd_rip.subtitles_lang);
mpv_set_option_string(dvd_mpv, "slang", dvd_rip.subtitles_lang);
} else if(strlen(dvd_rip.subtitles_stream_id)) {
fprintf(stderr, "[dvd_rip] burn-in subtitles stream '%s'\n", dvd_rip.subtitles_stream_id);
mpv_set_option_string(dvd_mpv, "sid", dvd_rip.subtitles_stream_id);
}
// ** All encoding and user config options must be set before initialize **
retval = mpv_initialize(dvd_mpv);
if(retval) {
fprintf(stderr, "[dvd_rip] mpv_initialize() failed\n");
return 1;
}
retval = mpv_command(dvd_mpv, dvd_mpv_commands);
if(retval) {
fprintf(stderr, "[dvd_rip] mpv_command() failed\n");
return 1;
}
// Print what settings are used at this point
/*
fprintf(stderr, "[libmpv] ovc = %s\n", mpv_get_property_string(dvd_mpv, "ovc"));
fprintf(stderr, "[libmpv] ovcopts = %s\n", mpv_get_property_string(dvd_mpv, "ovcopts"));
if(strlen(dvd_rip.vf_opts))
fprintf(stderr, "[libmpv] vf = %s\n", mpv_get_property_string(dvd_mpv, "vf"));
if(strlen(dvd_rip.audio_lang))
fprintf(stderr, "[libmpv] alang = %s\n", mpv_get_property_string(dvd_mpv, "alang"));
else if(strlen(dvd_rip.audio_stream_id))
fprintf(stderr, "[libmpv] aid = %s\n", mpv_get_property_string(dvd_mpv, "aid"));
fprintf(stderr, "[libmpv] acodec = %s\n", mpv_get_property_string(dvd_mpv, "oac"));
printf("[dvd_rip] mpv dvd://%" PRIu16 " # mpv zero-indexes tracks\n", dvd_rip.track - 1);
fprintf(stderr, "[libmpv] slang = %s\n", mpv_get_property_string(dvd_mpv, "slang"));
fprintf(stderr, "[libmpv] sid = %s\n", mpv_get_property_string(dvd_mpv, "sid"));
*/
mpv_set_option_string(dvd_mpv, "sid", "none");
mpv_event *dvd_mpv_event = NULL;
struct mpv_event_log_message *dvd_mpv_log_message = NULL;
struct mpv_event_end_file *dvd_mpv_eof = NULL;
retval = 0;
// Display output
if(x264 || x265)
fprintf(stderr, "[dvd_rip] using video codec '%s' and CRF '%i'\n", dvd_rip.vcodec, crf);
if(vp8 || vp9)
fprintf(stderr, "[dvd_rip] using video codec '%s' and bitrate '%ik'\n", dvd_rip.vcodec, dvd_rip.video_bitrate);
if(verbose) {
fprintf(stderr, "[dvd_rip] mpv 'vcodecopts=%s'\n", dvd_rip.vcodec_opts);
fprintf(stderr, "[dvd_rip] mpv 'acodecopts=%s'\n", dvd_rip.acodec_opts);
}
if(detelecine)
printf("[dvd_rip] detelecining video using 'pullup', 'dejudder', and 'fps' filters\n");
fprintf(stderr, "[dvd_rip] using audio codec '%s' and bitrate '%" PRIu16 "k'\n", dvd_rip.acodec, dvd_rip.audio_bitrate);
while(true) {