Compare commits

...

1 Commits

Author SHA1 Message Date
Y 09edd73292 wip… 2017-11-06 23:13:06 +01:00
1 changed files with 98 additions and 55 deletions

View File

@ -1,6 +1,6 @@
#!/usr/bin/bash
# Needed:
# ffmpeg, ffprobe, mkvmerge, x264, bash, bc, awk, sed, sort, uniq, tr
# ffmpeg, ffprobe, mkvmerge, x264, bash, bc, gawk, sed, sort, uniq, tr
# Mandatory:
# -i <file> [-i <file> …] Input files
# Optional:
@ -96,8 +96,11 @@ IFS=$'\n' read -d '' -a f_info < <(
done
)
# editable fields are (1-based) #17 and #18
V_EDIT="17:language,18:title"
# fields: int:file index, int:stream index, str:codec, str:profile, int:width,
# int:height, [str:transformation], [int:new width], [int:new height],
# int:height, [str:transformation], int:left crop, int:top crop,
# int:right crop, int:bottom crop, [int:new width], [int:new height],
# str:sample aspect ratio, str:fps, float:stream start time,
# str:language, [str:title], bool:use this stream
exec 3>&1
@ -169,6 +172,8 @@ IFS=$'\n' read -d '' -a v_info < <(
)
exec 3>&-
# editable fields are (1-based) #7, #13 and #14
A_EDIT="7:planned kb/s,13:language,14:title"
# fields: int:file index, int:stream index, str:codec, str:profile,
# int:sample frequency, int:number of channels, int:planned kb/s,
# str:layout, float:stream start time, bool:stream is default in file,
@ -202,6 +207,8 @@ IFS=$'\n' read -d '' -a a_info < <(
done
)
# editable fields are (0-based) #8 and #9
S_EDIT="8:language,9:title"
# fields: int:file index, int:stream index, str:codec, float:stream start time,
# bool:stream is default in file, bool:stream is forced in file,
# bool:stream is for hearing impaired, str:language, [str:title],
@ -228,7 +235,7 @@ IFS=$'\n' read -d '' -a s_info < <(
done
)
## PRINT INFORMATION, SELECT STREAMS
## PRINT INFORMATION, SELECT/EDIT STREAMS
# $1:array, $2:list-program (awk)
function print_info() {
@ -236,18 +243,41 @@ function print_info() {
printf '%s\n' "${arr[@]}" | awk -F$'\t' -vOFS=$'\t' "$2"
}
# $1:array, $2:comma-separated list of editable fields
function edit_fields() {
local l=
local -n arr=$1
read -e -p 'Number of the line to edit: ' l
l=${l//[^0-9]}
[ -n "$l" ] && [ $l -ge 0 ] && [ $l -lt ${#arr[*]} ] || return
exec 3>&1 4<&0
arr[$l]="$(gawk -F$'\t' -vOFS=$'\t' -vlist="$2" '{
split(list,fields,",")
for (i in fields) {
split(fields[i],n_d,":"); n=n_d[1]; d=n_d[2]
printf "Replacement value for “%s” (%s): ", $n, d >"/dev/fd/3"
getline v <"/dev/fd/4"
$n=v
}
print
}' <<<"${arr[$l]}")"
exec 3>&- 4<&-
}
# $1:array, $2:question, $3:default answer, $4:field to set,
# $5==1 if only one flag can be set,
# $6>0 means that the flag with this number must be set in the parent file, too.
# exit status == 1 if a line-edit is requested
function set_flags() {
local -n arr=$1
local choice="$3"
local c=
local f=
if [ -z "$defaults" ]; then
read -e -i "$choice" -p "$2" choice
read -e -i "$choice" -p "$2 (or “e” to edit a line): " choice
[ "$choice" == 'e' ] && return 1
else
echo "$2$choice"
echo "$2: $choice"
fi
for c in $choice; do
c=${c//[^0-9]}
@ -259,66 +289,79 @@ function set_flags() {
awk -F$'\t' -vOFS=$'\t' -vf=$6 '{$f=1;print}' <<<"${f_info[$f]}")"
fi
fi
[ "$5" == "1" ] && break
# `continue` ensures a “0” return value if $5!=1
[ "$5" == "1" ] && break || continue
done
}
if [ ${#f_info[*]} -gt 0 ]; then
echo FILES:
print_info f_info '{ print NR-1, $1, $4==0?"":("(with " $4 " chapters)") }'
if [ -z "$abort" ]; then
set_flags f_info 'File from which chapters should be read (if any): ' \
"$(printf '%s\n' "${f_info[@]}" \
| awk -F$'\t' '$4>0{print NR-1; exit}')" 2 1
fi
while true; do
echo FILES:
print_info f_info '{ print NR-1, $1, $4==0?"":("(with " $4 " chapters)") }'
if [ -z "$abort" ]; then
set_flags f_info 'File from which chapters should be read (if any)' \
"$(printf '%s\n' "${f_info[@]}" \
| awk -F$'\t' '$4>0{print NR-1; exit}')" 2 1
fi
[ $? -eq 0 ] && break || printf '\nNo editable fields for files.\n\n' >&2
done
fi
if [ ${#v_info[*]} -gt 0 ]; then
echo VIDEO:
print_info v_info '
BEGIN {print "scale=2" |& "bc -q"}
{ sub(/.$/,"",$7)
print $15 |& "bc -q"; "bc -q" |& getline fps
print NR-1, "file " $1, $3 ($4==""?"":(" (" $4 ")")), $5 "×" $6, \
$7==""?"":($7 $12 "×" $13), fps "fps", $17, $18 }'
if [ -z "$abort" ]; then
set_flags v_info 'Space-separated list of video streams to include: ' \
"$(eval echo \{0..$((${#v_info[*]}-1))\})" 19
fi
while true; do
echo VIDEO:
print_info v_info '
BEGIN {print "scale=2" |& "bc -q"}
{ sub(/.$/,"",$7)
print $15 |& "bc -q"; "bc -q" |& getline fps
print NR-1, "file " $1, $3 ($4==""?"":(" (" $4 ")")), $5 "×" $6, \
$7==""?"":($7 $12 "×" $13), fps "fps", $17, $18 }'
if [ -z "$abort" ]; then
set_flags v_info 'Space-separated list of video streams to include' \
"$(eval echo \{0..$((${#v_info[*]}-1))\})" 19
fi
[ $? -eq 0 ] && break || edit_fields v_info "$V_EDIT"
done
fi
if [ ${#a_info[*]} -gt 0 ]; then
echo AUDIO:
print_info a_info '
{ print NR-1, "file " $1, $3 ($4==""?"":(" (" $4 ")")), \
$6 "×@" $5 "Hz", $8, $7 "kb/s", $13, $14, \
$10=="1"?"(default track)":"", $11=="1"?"(forced track)":"", \
$12=="1"?"(vision impaired)":"" }'
if [ -z "$abort" ]; then
set_flags a_info 'Space-separated list of audio streams to include: ' \
"$(eval echo \{0..$((${#a_info[*]}-1))\})" 15
set_flags a_info 'Default audio stream (if any): ' \
"$(printf '%s\n' "${a_info[@]}" \
| awk -F$'\t' '$10==1&&$15==1{print NR-1}' | head -n 1)" 16 1
set_flags a_info 'Forced audio streams (if any): ' \
"$(printf '%s\n' "${a_info[@]}" \
| awk -F$'\t' -vORS=' ' '$11==1&&$15==1{print NR-1}')" 17
fi
while true; do
echo AUDIO:
print_info a_info '
{ print NR-1, "file " $1, $3 ($4==""?"":(" (" $4 ")")), \
$6 "×@" $5 "Hz", $8, $7 "kb/s", $13, $14, \
$10=="1"?"(default track)":"", $11=="1"?"(forced track)":"", \
$12=="1"?"(vision impaired)":"" }'
if [ -z "$abort" ]; then
set_flags a_info 'Space-separated list of audio streams to include' \
"$(eval echo \{0..$((${#a_info[*]}-1))\})" 15 \
&& set_flags a_info 'Default audio stream (if any)' \
"$(printf '%s\n' "${a_info[@]}" \
| awk -F$'\t' '$10==1&&$15==1{print NR-1}' | head -n 1)" 16 1 \
&& set_flags a_info 'Forced audio streams (if any)' \
"$(printf '%s\n' "${a_info[@]}" \
| awk -F$'\t' -vORS=' ' '$11==1&&$15==1{print NR-1}')" 17
fi
[ $? -eq 0 ] && break || edit_fields a_info "$A_EDIT"
done
fi
if [ ${#s_info[*]} -gt 0 ]; then
echo SUBTITLES:
print_info s_info '
{ print NR-1, "file " $1, $3, $8, $9, \
$5=="1"?"(default track)":"", $6=="1"?"(forced track)":"", \
$7=="1"?"(hearing impaired)":"" }'
if [ -z "$abort" ]; then
set_flags s_info 'Space-separated list of subtitle streams to include: ' \
"$(eval echo \{0..$((${#s_info[*]}-1))\})" 10 0 3
set_flags s_info 'Default subtitle stream (if any): ' \
"$(printf '%s\n' "${s_info[@]}" \
| awk -F$'\t' '$5==1&&$10==1{print NR-1}' | head -n 1)" 11 1
set_flags s_info 'Forced subtitle streams (if any): ' \
"$(printf '%s\n' "${s_info[@]}" \
| awk -F$'\t' -vORS=' ' '$6==1&&$10==1{print NR-1}')" 12
fi
while true; do
echo SUBTITLES:
print_info s_info '
{ print NR-1, "file " $1, $3, $8, $9, \
$5=="1"?"(default track)":"", $6=="1"?"(forced track)":"", \
$7=="1"?"(hearing impaired)":"" }'
if [ -z "$abort" ]; then
set_flags s_info 'Space-separated list of subtitle streams to include' \
"$(eval echo \{0..$((${#s_info[*]}-1))\})" 10 0 3 \
&& set_flags s_info 'Default subtitle stream (if any)' \
"$(printf '%s\n' "${s_info[@]}" \
| awk -F$'\t' '$5==1&&$10==1{print NR-1}' | head -n 1)" 11 1 \
&& set_flags s_info 'Forced subtitle streams (if any)' \
"$(printf '%s\n' "${s_info[@]}" \
| awk -F$'\t' -vORS=' ' '$6==1&&$10==1{print NR-1}')" 12
fi
[ $? -eq 0 ] && break || edit_fields s_info "$S_EDIT"
done
fi
[ -z "$abort" ] || exit 0