forked from mediamicroservices/mm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makelossless
executable file
·105 lines (98 loc) · 4.47 KB
/
makelossless
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
#!/bin/bash
# makelossless
version="1.0"
scriptdir=$(dirname $(which "${0}"))
. "${scriptdir}/mmfunctions" || { echo "Missing '${scriptdir}/mmfunctions'. Exiting." ; exit 1 ;};
dependencies=(ffmpeg ffprobe mediainfo)
_initialize_make
usage(){
echo
echo "$(basename "${0}") ${version}"
echo "This application will losslessly transcode a video file or package input with the following options."
echo "Dependencies: ${dependencies[@]}"
echo "Usage: $(basename $0) fileorpackage1 [ fileorpackage2 ...]"
echo " -j (use lossless jpeg2000 instead of ffv1 version 3)"
echo " -n (dry-run mode, show the commands that would be run but don't do anything)"
echo " -h display this help"
echo
exit
}
[ "${#}" = 0 ] && usage
# command-line options to set mediaid and original variables
OPTIND=1
while getopts ":jnh" opt ; do
case "${opt}" in
j) jpeg2000mode="Y";;
n) DRYRUN=true;;
h) usage ;;
*) echo "bad option -$OPTARG" ; usage ;;
:) echo "Option -$OPTARG requires an argument" ; exit 1 ;;
esac
done
shift $(( ${OPTIND} - 1 ))
while [ "${*}" != "" ] ; do
# get context about the input
input="${1}"
[ -d "${input}" ] && logdir="${input}/metadata/submissionDocumentation/logs"
[ -f "${input}" ] && logdir="$(dirname "${input}")/lossless/logs"
[ ! "${logdir}" ] && logdir="${input}/metadata/submissionDocumentation/logs"
find "${input}" -type f \( -iname "*.mov" -o -iname "*.avi" -o -iname "*.mxf" \) -print0 | while read -d $'\0' sourcefile ; do
outputdir=$(dirname "${sourcefile}")
sourcefilename=$(basename "${sourcefile}")
_log -b
# clear local arrays
unset inputoptions
unset inputoptionsframemd5
unset middleoptions
# encoding options
get_codectagstring "${sourcefile}"
get_videostreamcount "${sourcefile}"
get_audiostreamcount "${sourcefile}"
inputoptions+=(-nostdin)
inputoptions+=(-vsync 0)
if [[ "${VIDEOSTREAMCOUNT}" > 0 ]] ; then
if [[ "${codec_tag_string}" == "mjp2" ]] ; then
inputoptions+=(-vcodec libopenjpeg)
fi
middleoptions+=(-map 0:v)
middleoptions+=(-map 0:a)
if [[ "${jpeg2000mode}" == "Y" ]] ; then
middleoptions+=(-c:v libopenjpeg)
inputoptionsframemd5+=(-vcodec libopenjpeg)
suffix="_j2k"
else
middleoptions+=(-c:v ffv1 -level 3 -g 1)
suffix="_ffv1"
fi
middleoptions+=(-vf setfield=bff) # this is a presumption but much of the uncompressed input is bff but not probably labelled
fi
if [[ "${AUDIOSTREAMCOUNT}" > 0 ]] ; then
middleoptions+=(-c:a copy)
fi
get_codectagstring "${sourcefile}"
_run mkdir -p "${outputdir}" "${logdir}"
if [ "${codec_tag_string}" == "2vuy" -o "${codec_tag_string}" == "v210" ] ; then
report -dt "${sourcefilename} is ${codec_tag_string}, starting encode"
_prep_ffmpeg_log
_run_critical ffmpeg ${inputoptions[@]} -i "${sourcefile}" ${middleoptions[@]} "${outputdir}/${sourcefilename%.*}${suffix}.mov" -f framemd5 -an "${logdir}/${sourcefilename%.*}.framemd5"
if [ "${codec_tag_string}" == "2vuy" ] ; then
_run_critical ffmpeg ${inputoptions[@]} -i "${outputdir}/${sourcefilename%.*}${suffix}.mov" -f framemd5 -pix_fmt uyvy422 -an "${logdir}/${sourcefilename%.*}${suffix}.framemd5"
else
_run_critical ffmpeg ${inputoptions[@]} ${inputoptionsframemd5[@]} -i "${outputdir}/${sourcefilename%.*}${suffix}.mov" -f framemd5 -an "${logdir}/${sourcefilename%.*}${suffix}.framemd5"
fi
if [ $(grep -v "^#" "${logdir}/${sourcefilename%.*}.framemd5" | md5 -q) = $(grep -v "^#" "${logdir}/${sourcefilename%.*}${suffix}.framemd5" | md5 -q) ] ; then
report -dt "Everything looks safe. Going to delete the original."
_run_critical mediainfo -f --language=raw --output=XML "${sourcefile}" > "${logdir}/${sourcefilename%.*}_mediainfo.xml"
_run_critical rm -f -v "${sourcefile}"
else
report -wt "Not looking safe. Going to keep the original."
fi
else
report -wt "${sourcefilename} is not 2vuy or v210 or progressive ffv1, skipping"
continue
fi
report -dt done with "${sourcefile}"
done
shift
_log -e
done