Skip to content

Commit 6187330

Browse files
committed
Further improvement of borders detection
1 parent ec2a627 commit 6187330

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

yt-dlp_discord

+26-12
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ target_audio_bitrate=96 # 128kbps target audio bitrate
1313
audio_only=0
1414
crop=0
1515
cropvalue="crop=iw:ih:(in_w-out_w)/2:(in_h-out_h)/2"
16+
cropdetect_params_bb="20:16:0"
17+
cropdetect_params_wb="3:16:0"
1618
verbose="--no-warnings"
1719
ffmpeg_enable=1
1820
ffmpeg_loglevel="-loglevel quiet -stats"
@@ -89,24 +91,34 @@ file_no_ext=$(find .tmp -type f | cut -d "/" -f 2 | cut -d "." -f 1)
8991

9092
if [[ "$crop" = "1" ]]
9193
then
92-
echo -e "[FFmpeg] ${YELLOW}Auto-cropping enabled.${ENDCOL}"
94+
echo "[FFmpeg] Auto-cropping enabled."
95+
96+
baseres=$(ffmpeg -i ".tmp/$file" -t 1 -vf "cropdetect=0:0:0" -f null - 2>&1 | awk '/crop/ { print $NF }' | tail -1)
97+
9398
# Detect black bars
94-
cropvalue_bb_initial=$(ffmpeg -i ".tmp/$file" -t 1 -vf "cropdetect=20:2:0" -f null - 2>&1 | awk '/crop/ { print $NF }' | tail -1)
95-
cropvalue_bb_seek=$(ffmpeg -ss 5 -i ".tmp/$file" -t 1 -vf "cropdetect=20:2:0" -f null - 2>&1 | awk '/crop/ { print $NF }' | tail -1)
99+
cropvalue_bb_initial=$(ffmpeg -ss 1 -i ".tmp/$file" -t 1 -vf "cropdetect=${cropdetect_params_bb}" -f null - 2>&1 | awk '/crop/ { print $NF }' | tail -1)
100+
cropvalue_bb_seek=$(ffmpeg -ss 6 -i ".tmp/$file" -t 1 -vf "cropdetect=${cropdetect_params_bb}" -f null - 2>&1 | awk '/crop/ { print $NF }' | tail -1)
96101

97102
# Detect white bars
98-
cropvalue_wb_initial=$(ffmpeg -i ".tmp/$file" -t 1 -vf "lutrgb=r=negval:g=negval:b=negval, cropdetect=20:2:0" -f null - 2>&1 | awk '/crop/ { print $NF }' | tail -1)
99-
cropvalue_wb_seek=$(ffmpeg -ss 5 -i ".tmp/$file" -t 1 -vf "lutrgb=r=negval:g=negval:b=negval, cropdetect=20:2:0" -f null - 2>&1 | awk '/crop/ { print $NF }' | tail -1)
103+
cropvalue_wb_initial=$(ffmpeg -ss 1 -i ".tmp/$file" -t 1 -vf "lutrgb=r=negval:g=negval:b=negval, cropdetect=${cropdetect_params_wb}" -f null - 2>&1 | awk '/crop/ { print $NF }' | tail -1)
104+
cropvalue_wb_seek=$(ffmpeg -ss 6 -i ".tmp/$file" -t 1 -vf "lutrgb=r=negval:g=negval:b=negval, cropdetect=${cropdetect_params_wb}" -f null - 2>&1 | awk '/crop/ { print $NF }' | tail -1)
100105

101-
# If the calculated crop-value is identical from the start of the video and 5 seconds in, apply crop value.
106+
# If calculated crop-value is identical value both from the start and 6 seconds in, and the values are different from base resolution
102107
if [[ "$cropvalue_bb_initial" = "$cropvalue_bb_seek" ]]
103108
then
104-
cropvalue=$cropvalue_bb_seek
105-
fi
106-
107-
if [[ "$cropvalue_wb_initial" = "$cropvalue_wb_seek" ]]
108-
then
109-
cropvalue=$cropvalue_wb_seek
109+
if [[ "$cropvalue_bb_initial" != "$baseres" ]]
110+
then
111+
cropvalue=$cropvalue_bb_initial
112+
fi
113+
114+
# If the inverted crop-value detection is not base resolution, apply crop based on white borders instead.
115+
if [[ "$cropvalue_wb_initial" = "$cropvalue_wb_seek" ]]
116+
then
117+
if [[ "$cropvalue_wb_initial" != "$baseres" ]]
118+
then
119+
cropvalue=$cropvalue_wb_initial
120+
fi
121+
fi
110122
fi
111123
fi
112124

@@ -159,6 +171,7 @@ if [[ "$ffmpeg_enable" = "1" ]]
159171
then
160172
echo "[FFmpeg] Converting video..."
161173
ffmpeg $ffmpeg_loglevel -i ".tmp/$file" $postprocessor_args_str "$file_no_ext.mp4" && rm ".tmp/$file" && rm -rf .tmp
174+
echo -e "[FFmpeg] File saved as: '${YELLOW}$file_no_ext.mp4${ENDCOL}'"
162175
fi
163176

164177
# Print debug info if verbose is enabled
@@ -173,6 +186,7 @@ then
173186
echo -e "${YELLOW}Total bitrate (bits):${ENDCOL} $total_bitrate"
174187
echo -e "${YELLOW}Video bitrate (bits):${ENDCOL} $video_bitrate"
175188
echo -e "${YELLOW}Audio bitrate (bits):${ENDCOL} $audio_bitrate"
189+
echo -e "${YELLOW}Base resolution:${ENDCOL} $baseres"
176190
echo -e "${YELLOW}Cropvalue black bars initial:${ENDCOL} $cropvalue_bb_initial"
177191
echo -e "${YELLOW}Cropvalue black bars seek:${ENDCOL} $cropvalue_bb_seek"
178192
echo -e "${YELLOW}Cropvalue white bars initial:${ENDCOL} $cropvalue_wb_initial"

0 commit comments

Comments
 (0)