-3

I have a simple script to resize images:

#!/usr/bin/bash

right_size='/home/scripts/images_correct_size'
user=$(whoami)

case $user in
    'sonarr')
        folder='/var/lib/sonarr/MediaCover'
        ;;
    'radarr')
        folder='/var/lib/radarr/MediaCover'
        ;;
    'jellyfin')
        folder='/var/lib/jellyfin/metadata'
        ;;
    *)
        echo "This user cannot run the script."
        ;;
esac

echo "Running script as $user."

find $folder -type f -name "*.jpg" | while IFS= read -r line
do
    echo "Current file: $line"
    # Grep has a match
    if grep -q $line $right_size; then
        :
    else
        size=$(ffprobe -hide_banner -v error -select_streams v:0 -show_entries stream=width,height -of csv=p=0 $line)
        width=$(cut -d ',' -f 1 <<<$size)
        height=$(cut -d ',' -f 2 <<<$size)

        if [[ $width -gt 1024 ]] && [[ $width -ge $height ]]; then
            echo "File width too large, reducing."
            ffmpeg -hide_banner -y -i $line -vf scale=1024:-1 $line > /dev/null 2>&1
            echo $line >> $right_size
            continue
        elif [[ $height -gt 1024 ]] && [[ $height -ge $width ]]; then
            echo "File width too large, reducing."
            ffmpeg -hide_banner -y -i $line -vf scale=-1:1024 $line > /dev/null 2>&1
            echo $line >> $right_size
            continue
        else 
            echo "File is right, nothing to do."
            echo $line >> $right_size
        fi
    fi
done

When it runs and the ffmpeg gets executed, for the next line the the variable line does not have the inital /, rendering the script useless for one iteration. In the next iteration of the loop, everything is back to normal. This is part of the output, where the problem is visible:

Current file: /var/lib/radarr/MediaCover/154/fanart-180.jpg
File is right, nothing to do.
Current file: /var/lib/radarr/MediaCover/154/fanart.jpg
File width too large, reducing.
Input #0, image2, from '/var/lib/radarr/MediaCover/154/fanart.jpg':
  Duration: 00:00:00.04, start: 0.000000, bitrate: 78855 kb/s
    Stream #0:0: Video: mjpeg (Progressive), yuvj420p(pc, bt470bg/unknown/unknown), 3840x2160 [SAR 1:1 DAR 16:9], 25 tbr, 25 tbn, 25 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (mjpeg (native) -> mjpeg (native))
Press [q] to stop, [?] for help
[swscaler @ 0x5603ac596680] deprecated pixel format used, make sure you did set range correctly
Output #0, image2, to '/var/lib/radarr/MediaCover/154/fanart.jpg':
  Metadata:
    encoder         : Lavf58.45.100
    Stream #0:0: Video: mjpeg, yuvj420p(pc), 1024x576 [SAR 1:1 DAR 16:9], q=2-31, 200 kb/s, 25 fps, 25 tbn, 25 tbc
    Metadata:
      encoder         : Lavc58.91.100 mjpeg
    Side data:
      cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: N/A
frame=    1 fps=0.0 q=7.0 Lsize=N/A time=00:00:00.04 bitrate=N/A speed=0.754x    
video:38kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
Current file: var/lib/radarr/MediaCover/154/poster-500.jpg
var/lib/radarr/MediaCover/154/poster-500.jpg: No such file or directory
File is right, nothing to do.
Current file: /var/lib/radarr/MediaCover/3/poster.jpg

Why is this happening?

oguz ismail
  • 1
  • 16
  • 47
  • 69
Keizer
  • 23
  • 2
  • 1
    Please paste your script first at [shellcheck.net](http://www.shellcheck.net/) and try to implement the recommendations made there. – Cyrus Aug 01 '20 at 13:58
  • I'm guessing something inside the `while read` loop consumes one byte from standard input, probably in response to something like a `y/n` prompt. Try redirecting stdin from `/dev/null` on selected lines (my primary suspect would be `ffprobe`) – tripleee Aug 01 '20 at 14:15
  • Indeed, it was `ffmpeg` consuming one byte. Running it with the `-nosdtin` as suggested here [link](https://github.com/koalaman/shellcheck/wiki/SC2095) solves the issue. – Keizer Aug 01 '20 at 14:30

1 Answers1

0

The ffmpeg commands consumes one byte as per shellcheck.net. Running it with the -nostdin option solves the problem.

Keizer
  • 23
  • 2