-
Notifications
You must be signed in to change notification settings - Fork 0
/
dvdmovie
executable file
·94 lines (88 loc) · 3.6 KB
/
dvdmovie
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
#! /usr/bin/env bash
# dvdmovie plays the movie from a video DVD using MPlayer
#
# Copyright (C) 2012-2022 Erik Auerswald <[email protected]>
#
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved. This file is offered as-is,
# without any warranty.
# dvdmovie finds the longest title on a DVD and plays it (using MPlayer)
# additional MPlayer options, e.g. -dvd-device ..., can be specified
# per DVD settings can be saved in $HOME/.dvdmovierc
# $HOME/.dvdmovierc consists of lines containing semicolon separated fields
# the fields are (in this order): Disc ID, Movie Title, Track, Options
# every field besides the Disc ID may be empty
# version 2022-09-24-01
set -u
RC="${HOME}/.dvdmovierc"
test -r "$RC" || {
RC="${XDG_CONFIG_HOME:-$HOME/.config}/dvdmovierc"
# a relative XDG_CONFIG_HOME must be ignored
test "${RC:0:1}" = '/' || RC="${HOME}/.config/dvdmovierc"
}
DVDMOVIERC_TITLE=''
DVDMOVIERC_OPTS=''
MPLAYER="${HOME}/bin/mplayer"
test -x "$MPLAYER" || MPLAYER=$(which mplayer 2> /dev/null)
test -x "$MPLAYER" || MPLAYER=$(type mplayer | awk '{print $NF}')
test -x "$MPLAYER" || { echo 'Could not find mplayer, exiting'; exit 1; }
IOPTS='-cache-min 0 -vo null -ao null -frames 0 -really-quiet -identify'
MOPTS='-really-quiet'
DISC='dvd://'
printf -- 'Looking for movie title on DVD, this can take some time...'
# specifying a DVD device requires "$@"
DVDMOVIE_ARRAY=($("$MPLAYER" $IOPTS "$@" "$DISC" 2> /dev/null | awk '
BEGIN {title = 0; len = 0; discid = 0; name = 0}
/ID_DVD_TITLE_[0-9]+_LENGTH/ {
gsub("ID_DVD_TITLE_","")
gsub("_LENGTH="," ")
split($0,A)
if(A[2] > len) {title = A[1]; len = A[2]}
}
/ID_DVD_DISC_ID=[0-9a-fA-F]+/ {
split($0,A,"=")
discid = A[2]
}
/ID_DVD_VOLUME_ID=/ {
split($0,A,"=")
name = A[2]
}
END {printf "%d %.0f %s %s\n", title, len/60, discid, name}
'))
echo 'done.'
DVDMOVIE_TITLE=${DVDMOVIE_ARRAY[0]}
DVDMOVIE_LEN=${DVDMOVIE_ARRAY[1]}
DVDMOVIE_DISCID=${DVDMOVIE_ARRAY[2]}
DVDMOVIE_NAME=${DVDMOVIE_ARRAY[3]}
# add whitespace separated DVD name components
for ((I=4; I<${#DVDMOVIE_ARRAY[*]}; I+=1)); do
DVDMOVIE_NAME="$DVDMOVIE_NAME ${DVDMOVIE_ARRAY[$I]}"
done
test "$DVDMOVIE_TITLE" -gt 0 ||
{ echo 'ERROR: Could not read titles of DVD.' ; exit 1; }
test "$DVDMOVIE_LEN" -gt 0 ||
{ echo "ERROR: Longest title $DVDMOVIE_TITLE has zero length.";exit 1; }
test "$DVDMOVIE_DISCID" != '0' ||
echo 'WARNING: Could not determine disc ID of DVD.'
test "$DVDMOVIE_NAME" != '0' || DVDMOVIE_NAME='no name'
echo "Longest title on DVD ${DVDMOVIE_DISCID} ($DVDMOVIE_NAME) is no." \
"$DVDMOVIE_TITLE ($DVDMOVIE_LEN min.)."
test -r "$RC" && test "$DVDMOVIE_DISCID" != '0' &&
DVDMOVIERC_NAME="$(grep "^${DVDMOVIE_DISCID}" "$RC" | cut -d\; -f2)" &&
DVDMOVIERC_TITLE="$(grep "^${DVDMOVIE_DISCID}" "$RC" | cut -d\; -f3)" &&
DVDMOVIERC_OPTS="$(grep "^${DVDMOVIE_DISCID}" "$RC" | cut -d\; -f4)"
test "$DVDMOVIE_DISCID" != '0' && test -n "$DVDMOVIERC_NAME" &&
echo "Found $RC entry '$DVDMOVIERC_NAME'."
test "$DVDMOVIE_DISCID" != '0' && test -n "$DVDMOVIERC_TITLE" &&
echo "Using title $DVDMOVIERC_TITLE from $RC instead of longest." &&
DVDMOVIE_TITLE="$DVDMOVIERC_TITLE"
test "$DVDMOVIE_DISCID" != '0' && test -n "$DVDMOVIERC_OPTS" &&
echo "Adding options $DVDMOVIERC_OPTS from ${RC}." &&
MOPTS="$MOPTS $DVDMOVIERC_OPTS"
test -n "$*" &&
echo "Adding options $* from command line." &&
MOPTS="$MOPTS $*"
printf -- 'Playing DVD title %s with options %s [%s].\n' \
"$DVDMOVIE_TITLE" "$MOPTS" "$MOPTS ${DISC}${DVDMOVIE_TITLE}"
exec "$MPLAYER" $MOPTS "${DISC}${DVDMOVIE_TITLE}" 2> /dev/null