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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
|
#!/bin/sh
#################################################################
# #
# this scrip handles the queue which is generated from #
# the vdr-plugin vdrrip. #
# #
# usage: queuehandler queuefile tempdir #
# #
# #
# written by herbert attenberger <herbsl@a-land.de> #
# #
# 15.07.2003: 0.1.0: - initial version #
# 24.07.2003: 0.1.1: - added split function #
# 28.07.2003: 0.1.2: - added video codec "divx4/divx5" #
# 22.08.2003: 0.1.3: - added vdrecho function #
# 07.09.2003: 0.1.4: - added (preview) to the output filename #
# in preview mode #
# 15.09.2003: 0.1.5: - added -frames 0 to get -identify with #
# mplayer1.0pre1 working (thx to ronny) #
# 24.09.2003: 0.1.6: - added (optional) svdrpsend.pl in #
# function vdrecho #
# - added dvd-handling #
# 28.09.2003: 0.1.7: - added handling of postprocess-filters #
# - added rename function (thx to #
# memed@www.vdrportal.de) #
# - copy 001.vdr if a symbolic link couldn't #
# be created (for fat-partitions) #
# (thx to memed@www.vdrportal.de) #
# 30.09.2003: 0.1.8: - added -sws 2 to xvid/divx5 encoding #
# 25.11.2003: 0.1.9: - moved loop function to read_queue #
# - added lock, unlock and wait_unlock #
# functions #
# - moved encode.vdrrip to the plugins-dir #
# 14.12.2003: 0.2.0: - added check_exe function #
# 16.12.2003: 0.2.1: - removed mod_quant from xvid encoding #
# 20.12.2003: 0.2.2: - moved pp-filters to the last position #
# - added log_error function to output the #
# errors to the syslog & stderr #
# - added log_info function to output the #
# infos to the syslog & stdout #
# - added log_debug function to output the #
# debug-infos infos to the syslog & stdout #
# 10.01.2004: 0.2.4: - renamed check function to initialize #
# - added ogg-vorbis, ac3-handling #
# - added ogm, matroska-handling #
# - moved configuration to the file #
# queuehandler.sh.conf #
# - improved error-handling #
# - added execute function #
# - added trap-command & _trap-function #
# 18.01.2004: 0.2.5: - fixed a bug if scaling is off (thx to #
# cosmo@www.vdrportal.de) #
# 21.01.2004: 0.2.6: - added option $useropts #
# - completed dvd-handling #
# 13.02.2004: 0.2.7: - changed some if statements to elif #
# - changed preview-handling #
# 10.03.2004: 0.2.8: - added calc_steps function #
# - write status to encode.vdrrip #
# 22.03.2004: 0.2.9: - moved 2-pass logiles to $tempdir #
# 26.03.2004: 0.3.0: - added evecho function #
# - added $mencoder_ac3 & $mplayer_ac3 #
# - added $qh_ver & qh_conf_ver #
# #
#################################################################
qh_ver="0.5"
initialize () {
#
# make initial checks
#
scriptname=`basename $0`
scriptdir=`dirname $0`
cfgfile="$scriptname.conf"
if [ -e "$scriptdir/$cfgfile" ]
then
. "$scriptdir/$cfgfile"
else
log_error "file $cfgfile not found in $scriptdir, aborting !" 1
fi
if [ "$1" -a "$2" ]
then
queuefile="$1"
tempdir="$2"
pluginsdir=`dirname "$queuefile"`
else
log_error "usage: $scriptname queuefile tempdir" 1
fi
mkdir -p "$tempdir"
if [ ! -d "$tempdir" ]
then
log_error "directory $tempdir doesn't exist, aborting !" 1
fi
if [ "$3" ]; then log_info "the option -preview isn't supported anymore"; fi
local pids=`pgrep -d" " "$scriptname"`
local pid1=`echo "$pids" | cut -d" " -f1`
local pid2=`echo "$pids" | cut -d" " -f2`
if [ "$pid1" != "$pid2" ]
then
log_error "$scriptname is already running with pid $pid1, aborting !" 2
fi
# now the 2-pass logfiles are created in $tempdir ;-)
# (because -passlogfile doesn't do its job for xvid)
cd "$tempdir"
}
read_queue () {
#
# wait for the queuefile and read the first line
#
while [ ! -e "$queuefile" ]
do
echo
echo "waiting for queuefile $queuefile"
echo "sleeping $check seconds"
echo
sleep $check
done
wait_unlock # wait until the queuefile is unlocked
lock # locks the queuefile
local saveifs="$IFS"
IFS=";"
read dir name filesize filenumbers vcodec br_video min_q max_q \
crop_w crop_h crop_x crop_y scale_w scale_h acodec br_audio audio_id \
ppvalues rename container preview < "$queuefile"
IFS=$saveifs
# there is an active encoding
echo "- reading queuefile..." > "$pluginsdir/encode.vdrrip"
unlock # unlocks the queuefile
# correct some data
name=`echo "$name" | sed "s/ /_/g"`
local name_length=`echo "$name" | wc -c`
if [ $name_length -gt 20 ]
then
shortname=`echo "$name" | cut -c 1-17`
shortname="$shortname..."
else
shortname="$name"
fi
if [ "$ppvalues" = "(null)" ]; then ppvalues=""; fi
dvd=`echo "$dir" | grep "^dvd://"`
if [ "$preview" = "1" ]
then
local mode="preview"
else
local mode="full"
fi
if [ -e ${dir}/001.vdr ]
then
recGeneration="old"
else
recGeneration="new"
fi
log_info "### start encoding movie $shortname in $mode mode ###"
log_debug "version of queuehandler: $qh_ver"
log_debug "version of queuehandler.conf: $qh_conf_ver"
log_debug "dir: $dir"
log_debug "name: $name"
log_debug "filesize: $filesize"
log_debug "filenumbers: $filenumbers"
log_debug "vcodec: $vcodec"
log_debug "br_video: $br_video"
log_debug "min_q: $min_q"
log_debug "max_q: $max_q"
log_debug "crop_w: $crop_w"
log_debug "crop_h: $crop_h"
log_debug "crop_x: $crop_x"
log_debug "crop_y: $crop_y"
log_debug "scale_w: $scale_w"
log_debug "scale_h: $scale_h"
log_debug "acodec: $acodec"
log_debug "br_audio: $br_audio"
log_debug "audio_id: $audio_id"
log_debug "ppvalues: $ppvalues"
log_debug "rename: $rename"
log_debug "container: $container"
log_debug "preview: $preview"
log_debug "recGeneration: $recGeneration"
}
pre_check () {
#
# make some checks before the encoding
#
if [ "$error" ]; then return; fi
# check mplayer/mencoder
if [ ! "$dvd" -a $audio_id -eq 128 ]
then
# encoding a vdr-recording with selected ac3-stream
check_exe "$mencoder_ac3" "mencoder_ac3="
check_exe "$mplayer_ac3" "mplayer_ac3="
mc=$mencoder_ac3
mp=$mplayer_ac3
else
check_exe "$mencoder" "mencoder="
check_exe "$mplayer" "mplayer="
mc=$mencoder
mp=$mplayer
fi
# change the dvd-parameter to -dvd if the mencoder-version is 0.XX
if [ "$dvd" ]
then
local menc_ver=`$mc -v 2>/dev/null | grep "MEncoder" | sed "s/^MEncoder \(.\).*/\1/"`
if [ "$menc_ver" = "0" ]
then
dvd=`echo "$dvd" | sed "s/^dvd:\/\//-dvd /"`
fi
log_debug "dvd: $dvd"
fi
# check needed tools
case "$container" in
"avi")
if [ "$acodec" = "ogg-vorbis" ]
then
log_error "ogg-vorbis isn't a valid audio-codec for a avi-container"
fi
;;
"ogm")
check_exe "$projectx" "projectx="
check_exe "$ogmmerge" "ogmmerge="
if [ "$acodec" = "lame" -o "$acodec" = "ogg-vorbis" ]
then
check_exe "$ffmpeg" "ffmpeg="
fi
if [ $filenumbers -gt 1 ]
then
check_exe "$ogmsplit" "ogmsplit="
fi
;;
"matroska")
check_exe "$projectx" "projectx="
check_exe "$mkvmerge" "mkvmerge="
if [ "$acodec" = "lame" -o "$acodec" = "ogg-vorbis" ]
then
check_exe "$ffmpeg" "ffmpeg="
fi
;;
*)
log_error "unknown container $container"
;;
esac
}
calc_steps () {
#
# calculate the nuber of encoding-steps
#
if [ "$error" ]; then return
elif [ "$preview" = "1" ]
then
if [ "$container" = "avi" ]; then steps="2"
else steps="3"; fi
return
fi
steps="8"
if [ "$filenumbers" = "1" ]; then steps=$((steps-1)); fi
case "$container" in
"avi")
steps=$((steps-4))
if [ "$dvd" ]; then steps=$((steps-1))
elif [ ! -e "$dir/00002.ts" -o ! -e "$dir/002.vdr" ]; then steps=$((steps-1)); fi
;;
"ogm")
steps=$((steps-2))
if [ "$acodec" = "copy" -a $audio_id -eq 128 ]
then
steps=$((steps-1))
fi
;;
"matroska")
steps=$((steps-2))
if [ "$acodec" = "copy" ]; then steps=$((steps-1)); fi
;;
*)
;;
esac
}
preview () {
#
# get the preview values
#
if [ "$error" ]; then return
elif [ "$preview" != "1" ]; then return; fi
name="$name(preview)"
shortname="$shortname(preview)"
# start the preview in the middle of the movie
local prevfile
if [ $recGeneration = "old" ]; then prevfile="001.vdr"; else prevfile="00001.ts";fi
local length=`"$mp" -vo null -ao null -identify -frames 0 "$dir/$prevfile" 2>/dev/null | grep ID_LENGTH | cut -d"=" -f2`
local ss=$(($(echo $length | sed -e s/[.,].*//)/2))
previewval="-ss $ss -endpos $previewlength"
}
prepare() {
#
# prepares the encoding-process
#
if [ "$error" ]; then return
elif [ "$dvd" ]; then return; fi
# Recreate $tempdir if removed by vdr housekeeping
mkdir -p "$tempdir"
if [ ! -d "$tempdir" ]; then return; fi
cd "$tempdir"
case "$container" in
"avi")
# join all vdr-files to $tempdir/temp.vdr
local prevfile
if [ $recGeneration = "old" ]; then prevfile="002.vdr"; else prevfile="00002.ts";fi
if [ -e "$dir/$prevfile" -a "$preview" != "1" ]
then
log_info "joining all vdr-files from directory $dir"
evecho "joining vdr-files"
if [ $recGeneration = "new" ]
then
nice -+19 cat $dir/[0-9][0-9][0-9][0-9][0-9].ts > "$tempdir/temp.vdr"
else
nice -+19 cat $dir/[0-9][0-9][0-9].vdr > "$tempdir/temp.vdr"
fi
else
create_symbolic_link
fi
;;
"ogm"|"matroska")
# demux vdr-recording with projectx
if [ "$preview" = "1" ]
then
create_symbolic_link
else
log_info "demuxing all vdr-files from directory $dir"
evecho "demuxing vdr-files"
if [ $recGeneration = "new" ]
then
execute "$projectx -demux $dir/0*.ts -out $tempdir"
else
execute "$projectx -demux $dir/0*.vdr -out $tempdir"
mv $tempdir/001.m2v $tempdir/00001.m2v
mv $tempdir/001.mp2 $tempdir/00001.mp2
fi
# vdrsync 0.1.2.2 developer version creates bd.mpa
if [ -e "$tempdir/bd.mpa" ]
then
mv "$tempdir/bd.mpa" "$tempdir/bd.ac3"
fi
fi
;;
*)
;;
esac
}
create_symbolic_link() {
#
# creates a symbolic link of 00001.ts to $temdir/temp.vdr
#
if [ "$error" ]; then return
elif [ "$dvd" ]; then return; fi
log_info "create a symbolic link from $dir/ to $tempdir/temp.vdr"
if [ $recGeneration = "new" ]
then
ln -s "$dir/00001.ts" "$tempdir/temp.vdr"
else
ln -s "$dir/001.vdr" "$tempdir/temp.vdr"
fi
if [ ! -e "$tempdir/temp.vdr" ]
then
log_info "could not create a symolic link"
log_info "try to copy the file"
if [ $recGeneration = "new" ]
then
execute "cp $dir/00001.ts $tempdir/temp.vdr"
else
execute "cp $dir/001.vdr $tempdir/temp.vdr"
fi
fi
}
check_exe () {
#
# checks if $1 is a executable and exit the queuehandler with
# rc $3, if this one is set
#
if [ ! -x "$1" ]
then
if [ "$1" ]; then log_error "$1 isn't a executable"; fi
log_error "set a valid path for $2 in the file $scriptdir/$cfgfile." $3
fi
}
encode () {
#
# encodes the movie
#
if [ "$error" ]; then return; fi
# set mencoder audio values
case "$container" in
"avi")
case "$acodec" in
"lame")
if [ "$dvd" ]
then
local aopts="-oac mp3lame -lameopts br=$br_audio:abr:q=2:vol=8 -aid $audio_id"
else
local aopts="-oac mp3lame -lameopts br=$br_audio:abr:q=2 -aid $audio_id"
fi
;;
"copy")
local aopts="-oac copy -aid $audio_id"
;;
"ogg-vorbis")
# this shouldn't happen
;;
*)
log_error "unknown audio codec $acodec"
;;
esac
;;
"ogm")
local aopts="-nosound"
case "$acodec" in
"lame"|"ogg-vorbis")
if [ "$dvd" ]; then dump_audio_mplayer; fi
encode_ffmpeg
;;
"copy")
# convert mp2-files to ac3, because
# mp2 isn't supported by ogm
if [ "$dvd" ]; then dump_audio_mplayer; fi
if [ $audio_id -ge 129 ]; then encode_ffmpeg; fi
;;
*)
log_error "unknown audio codec $acodec"
;;
esac
;;
"matroska")
local aopts="-nosound"
case "$acodec" in
"lame"|"ogg-vorbis")
if [ "$dvd" ]; then dump_audio_mplayer; fi
encode_ffmpeg
;;
"copy")
if [ "$dvd" ]; then dump_audio_mplayer; fi
;;
*)
log_error "unknown audio codec $acodec"
;;
esac
;;
*)
;;
esac
# set mencoder -vf values
if [ "$crop_w" = "-1" -a "$crop_h" = "-1" -a "$crop_x" = "-1" -a \
"$crop_y" = "-1" -a "$scale_w" = "-1" -a "$scale_h" = "-1" ]
then
local vopopts=""
elif [ "$crop_w" = "-1" -a "$crop_h" = "-1" -a "$crop_x" = "-1" -a \
"$crop_y" = "-1" ]
then
local vopopts="scale=$scale_w:$scale_h"
else
local vopopts="crop=$crop_w:$crop_h:$crop_x:$crop_y,scale=$scale_w:$scale_h"
fi
if [ "$ppvalues" ]
then
local vopopts="-vf pp=$ppvalues,$vopopts"
elif [ "$vopopts" ]
then
local vopopts="-vf $vopopts"
fi
# encode in two passes
for pass in 1 2
do
if [ "$pass" = "1" ]
then
local ofile="-o /dev/null"
else
local ofile="-o $tempdir/$name.avi"
fi
# set mencoder video values
case "$vcodec" in
"lavc")
local vopts="-ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate=$br_video:vqmin=$min_q:vqmax=$max_q:vpass=$pass -sws 2"
;;
"xvid")
local vopts="-ovc xvid -xvidencopts bitrate=$br_video:me_quality=6:pass=$pass -sws 2"
;;
"divx4")
local vopts="-ovc divx4 -divx4opts br=$br_video:q=5:min_quant=$min_q:max_quant=$max_q:pass=$pass -sws 2"
;;
*)
log_error "unknown video codec $vcodec"
;;
esac
# set mencoder input file
if [ "$dvd" ]
then
# dvd
local ifile="$dvd"
elif [ "$container" = "avi" -o "$preview" = "1" ]
then
# no dvd, avi container
local ifile="$tempdir/temp.vdr"
else
# no dvd, ogm/matroska container
# search for the first video-file
local num=1
while [ ! "$ifile" ]
do
if [ ! -e "$tempdir/0000$num.m2v" ]
then
log_info "video-file $tempdir/0000$num.m2v not found !"
if [ $num -ge 9 ]
then
log_error "no video-stream found ! $num"
# exit loop
local ifile="dummy"
fi
num=$((num+1))
else
log_info "video-file $tempdir/0000$num.m2v found !"
local ifile="$tempdir/0000$num.m2v"
fi
done
fi
if [ "$error" ]; then return; fi
log_info "encoding movie $shortname (pass: $pass)"
if [ "$aopts" = "-nosound" ]
then
local encstr="video"
else
local encstr="video & audio"
fi
evecho "enc. $encstr ($pass. pass)"
if [ "$useropts" ]; then log_info "\$useropts are set to \"$useropts\""; fi
execute "$mc $ifile $ofile $useropts $vopts $vopopts $aopts $previewval"
done
}
dump_audio_mplayer() {
#
# dump audio-stream from dvd with mplayer
#
if [ "$error" ]; then return
elif [ "$preview" = "1" ]; then return
elif [ ! "$dvd" ]; then return; fi
log_info "dumping audio-stream $audio_id from $dvd with $mp"
evecho "dumping audio-stream from dvd"
execute "$mp $dir -vo null -vc dummy -aid $audio_id -aop list=volnorm -dumpaudio -dumpfile $tempdir/bd.ac3"
}
encode_ffmpeg() {
#
# encode the audio-stream with ffmpeg
#
if [ "$error" ]; then return
elif [ "$preview" = "1" ]; then return; fi
if [ $audio_id -eq 128 ]
then
local ifile="$tempdir/bd.ac3"
else
local ifile="$tempdir/00001.mp2"
local forceinput="-f mp3"
fi
case "$acodec" in
"lame")
local filetype="mp3"
local filecodec="libmp3lame"
;;
"ogg-vorbis")
local filetype="ogg"
local filecodec="libvorbis"
;;
"copy")
log_info "ogm doesn't support mp2 audio-streams, i will convert it into ac3"
local filetype="ac3"
local filecodec="ac3"
;;
*)
;;
esac
log_info "converting $ifile into $filetype-format"
evecho "conv. audio into $filetype-format"
execute "$ffmpeg -y $forceinput -i $ifile -ab ${br_audio}k -acodec $filecodec $tempdir/c$audio_id.$filetype"
}
merge_ogm_mkv() {
if [ "$error" ]; then return; fi
if [ "$container" != "ogm" -a "$container" != "matroska" ]; then return; fi
case "$container" in
"ogm")
local merge="$ogmmerge"
local filetype="ogm"
;;
"matroska")
local merge="$mkvmerge"
local filetype="mkv"
;;
*)
;;
esac
if [ "$preview" != "1" ]
then
case "$acodec" in
"lame")
local afile="$tempdir/c$audio_id.mp3"
;;
"ogg-vorbis")
local afile="$tempdir/c$audio_id.ogg"
;;
"copy")
if [ $audio_id -eq 128 ]
then
# ac3
local afile="$tempdir/bd.ac3"
elif [ "$container" = "ogm" ]
then
# mpg2, container ogm
local afile="$tempdir/c$audio_id.ac3"
else
# mpg2
local afile="$tempdir/c$audio_id.mpa"
fi
;;
*)
;;
esac
fi
log_info "merging movie $shortname into $filetype-container"
evecho "merging into $filetype-container"
execute "$merge -o $tempdir/$name.$filetype $tempdir/$name.avi $afile"
}
cleanup () {
#
# delete temp-files and reset variables
#
# temp. codec-files
rm -f "$tempdir/divx2pass.log"
rm -f "$tempdir/lavc_stats.txt"
rm -f "$tempdir/analyse.log"
rm -f "$tempdir/c:\\trace_b.txt"
rm -f "$tempdir/xvid-twopass.stats"
rm -f "$tempdir/00001_log.txt"
rm -f "$tempdir/001_log.txt"
# temp. movie-files
rm -f "$tempdir/temp.vdr"
if [ "$container" = "ogm" -o "$container" = "matroska" ]
then
rm -f "$tempdir/$name.avi"
fi
rm -f "$tempdir"/[0-9][0-9][0-9][0-9][0-9].m2v
rm -f "$tempdir"/[0-9][0-9][0-9][0-9][0-9].mp2
rm -f "$tempdir"/c*.mp3
rm -f "$tempdir"/c*.ogg
rm -f "$tempdir"/c*.ac3
rm -f "$tempdir"/bd.ac3
# temp. queuehandler-files
rm -f "$pluginsdir/encode.vdrrip"
rm -f /tmp/queuehandler.err
# reset variables
dir=""
name=""
filesize=""
filenumbers=""
vcodec=""
br_video=""
min_q=""
max_q=""
crop_w=""
crop_h=""
crop_x=""
crop_y=""
scale_w=""
scale_h=""
acodec=""
br_audio=""
audio_id=""
ppvalues=""
rename=""
container=""
preview=""
recGeneration=""
mc=""
mp=""
dvd=""
previewval=""
error=""
step="0"
}
del_queue () {
#
# delete first line from the queuefile
#
# waits until the queuefile is unlocked, and locks it
wait_unlock
lock
if [ "$error" ]
then
head -n 1 "$queuefile" >> "$queuefile.rejected"
fi
# delete first entry of the queuefile
local lines=`cat "$queuefile" | wc -l`
if [ "$lines" -le 1 ]
then
rm -f "$queuefile"
else
lines=$((lines-1))
cp -a "$queuefile" /tmp/queuefile.tmp
tail -n $lines /tmp/queuefile.tmp > "$queuefile"
fi
if [ "$error" ]
then
log_info "### moved movie $shortname to $queuefile.rejected ###"
else
log_info "### movie $shortname deleted from queuefile ###"
fi
# unlock queuefile
unlock
}
split () {
#
# splits the encoded movie into $filenumbers pieces
#
if [ "$error" ]; then return
elif [ "$filenumbers" = "1" -o "$preview" = "1" ]; then return; fi
log_info "splitting $shortname in $filenumbers pieces"
evecho "splitting movie"
case "$container" in
"avi")
local overlap=3
local count=1
local splitpos=0
# workaround to get the correct filesize from mencoder with -endpos
local splitsize=$((filesize*99/100))
log_info "splitting $shortname in $filenumbers pieces"
while [ $count -le $filenumbers ]
do
local ofile="$name-$count.avi"
if [ $count -eq $filenumbers ]
then
local endpos=""
else
local endpos="-endpos ${splitsize}mb"
fi
# split file
execute "$mc -ovc copy -oac copy $tempdir/$name.avi \
-ss $splitpos $endpos -o $tempdir/$ofile"
# detect length of splitted file and add it to $splitpos
local length=`$mplayer -vo null -ao null -identify -frames 0 $tempdir/$ofile 2>/dev/null | \
grep ID_LENGTH | cut -d= -f2`
splitpos=$((splitpos+length-overlap))
count=$((count+1))
done
;;
"ogm")
execute "$ogmsplit -s $filesize -n $filenumbers $tempdir/$name.ogm"
;;
"matroska")
execute "$mkvmerge --split d${filesize}m --split-max-files \
$filenumbers -o $tempdir/$ofile"
;;
*)
;;
esac
}
vdrecho () {
#
# echo $1 in the vdr-infobar (or console)
#
if [ -x "$svdrpsend" ]
then
$svdrpsend -d "$vdrhostname" "MESG $1" 2>/dev/null 1>/dev/null
elif [ -x "$netcat" ]
then
echo "MESG $1" | $netcat -q 1 "$vdrhostname" 2001 2>/dev/null 1>/dev/null
else
log_info "$1"
fi
}
evecho () {
#
# echo $ in the file encode.vdrrip
#
step=$((step+1))
local time=`date +"%k:%M h"`
echo "- step $step/$steps (since $time): $1" > "$pluginsdir/encode.vdrrip"
}
log_error () {
#
# echo $1 on stderr and write it to the syslog with priority user.error
#
logger -s -p user.error -t [vdrrip-qh] "$1"
if [ "$2" ]
then
vdrecho "$1"
exit "$2"
else
error="1"
fi
}
log_info () {
#
# echo $1 on stdout and write it to the syslog with priority user.info
#
logger -s -p user.info -t [vdrrip-qh] "$1" 2>&1
}
log_debug () {
#
# echo $1 on stdout and write it to the syslog with priority user.debug
#
if [ "$debug" = "1" ]
then
logger -s -p user.debug -t [vdrrip-qh] "$1"
fi
}
execute () {
#
# executes $1 and checks the rc
#
local cmd="$1"
log_debug "execute command: $cmd"
# execute $cmd and save the return-code:
if [ "$stdout" = "1" ]
then
nice -+19 $cmd 2>/tmp/queuehandler.err
local rc="$?"
else
nice -+19 $cmd 1>/dev/null 2>/tmp/queuehandler.err
local rc="$?"
fi
if [ "$rc" != "0" ]
then
# display stderr if the return-code isn't 0
log_error "an error occured (rc $rc) while processing the command: $cmd"
local err_message=`cat /tmp/queuehandler.err 2>/dev/null`
if [ "$err_message" ]
then
log_error "error message: $err_message"
fi
elif [ "$debug" = "1" ]
then
# display stderr if the debug-mode is set
local dbg_message=`cat /tmp/queuehandler.err 2>/dev/null`
if [ "$dbg_message" ]
then
log_debug "debug message: $dbg_message"
fi
fi
}
rename () {
#
# renames the vdr recording to indicate it was encoded properly
# this function is written by memed@www.vdrportal.de
#
if [ "$error" ]; then return
elif [ "$dvd" ]; then return
elif [ "$preview" = "1" ]; then return
elif [ "$rename" = "0" ]; then return; fi
new_dir="$(dirname $dir)${append_string}"
mkdir -p "$new_dir"
#move finished subdir to new folder
mv "$dir" "$new_dir"
#if all subdirs are empty, also delete dir one up
[ $(find $(dirname $dir) -type f|wc -l) -eq 0 ] && rm -rf $(dirname $dir)
}
wait_unlock () {
#
# wait until the queuefile is unlocked
#
local lockstat
read lockstat 2>/dev/null < "$pluginsdir/lock.vdrrip"
while [ -e "$pluginsdir/lock.vdrrip" -a "$lockstat" != "1" ]
do
echo
echo "queuefile is locked !"
echo "sleeping $lock seconds"
echo
sleep $lock
done
}
lock () {
#
# locks the queuefile
#
local lockfile="$pluginsdir/lock.vdrrip"
echo 1 > $lockfile 2>/dev/null
if [ ! -e "$lockfile" ]
then
log_error "couldn't write to $lockfile" 1
fi
}
unlock () {
#
# unlocks the queuefile
#
rm -f $pluginsdir/lock.vdrrip
if [ -e "$lockfile" ]
then
log_error "couldn't delete $lockfile" 1
fi
}
_trap () {
#
# this function is called if the queuehandler is killed
#
if [ "$debug" = "0" ]; then cleanup; fi
log_error "queuehandler was killed !"
vdrecho "queuehandler was killed !"
}
#
# this is the main part
#
trap "_trap; exit" 2 9 15
initialize "$1" "$2" "$3" # make initial checks
cleanup # delete old temp-files
while [ true ]
do
read_queue # read the first line from the queuefile
pre_check # make some checks before the encoding
calc_steps # calc the number of encoding-steps
preview # get the preview values
prepare # prepares the encoding
encode # encodes the movie
merge_ogm_mkv # merge ogm/mkv-files
split # splits the encoded movie
if [ ! "$error" ]
then
mess="movie $shortname encoded"
log_info "$mess"
vdrecho "$mess"
else
mess="couldn't encode movie $shortname"
log_info "$mess"
vdrecho "$mess"
fi
rename # rename dir-name of the vdr-recording
del_queue # delete first line from the queuefile
cleanup # delete temp-files
done
|