1

So i've written a bash script to take newly written to video files and transcode them for playback and streaming in my server here is the script

  while true; do
    printf -v filename '%q' "$(inotifywait --format "%w""%f" -r -e close_write /var/$
    echo $filename
            if [[ $filename == *".mp4"* ]]; then
                    echo "1"
                    avconv -i "$filename" "`echo ${filename%.mp4}.webm`"
            fi

            if [[ $filename == *".mkv"* ]]; then
                    echo "2"
                    avconv -i "$filename" "`echo ${filename%.mkv}.mp4`"
                    avconv -i "$filename" "`echo ${filename%.mkv}.webm`"
            fi

            if [[ "$filename" == *".avi"* ]]
                    then echo "3"
                    avconv -i "$filename" "`echo ${filename%.avi}.mp4`" &
                    avconv -i "$filename" "`echo ${filename%.avi}.webm`"&
                 fi
      done

The script works fairly well however if the $filename contains spaces avconv breaks and the script returns:

      Watches established.
      /var/www/media2net/tv/The\ Daily.mkv
      2
      avconv version 0.8.4-4:0.8.4-0ubuntu0.12.04.1, Copyright (c) 2000-2012 the Libav s
      built on Nov  6 2012 16:51:33 with gcc 4.6.3
       /var/www/media2net/tv/The\: No such file or directory
      avconv version 0.8.4-4:0.8.4-0ubuntu0.12.04.1, Copyright (c) 2000-2012 the Libav 
      built on Nov  6 2012 16:51:33 with gcc 4.6.3
      /var/www/media2net/tv/The\ Daily.mkv: No such file or directory
      Setting up watches.  Beware: since -r was given, this may take a while!
      Watches established.

however this is not correct as I can view the said file inside it's respective directory, I also have tested avconv with the following in the shell and everything works properly and avconv transcodes video

       avconv -i /var/www/media2net/The\ Daily.mkv /var/www/media2net/tv/The\ Daily.webm

Basically I am wondering what I am doing wrong in my script or if anyone else has experienced a similar problem using avconv or ffmpeg (as the 2 are fairly similar and avconv is ubuntu's fork of the project) any help would be greatly appreciated! Thanks in advance -brendan

4

1 回答 1

1

There seems to be some inconsistencies in your usage of escape backslashes and quotes. In fact, you only need one of the two, and the easier to use is certainly the quotes.

So, to remove the backslashes in filenames would be the first good thing to do.

Additionally,

"$(inotifywait --format "%w""%f"...

should be corrected in :

"$(inotifywait --format '%w%f'...

Also, the lines which comprehend something like :

"`echo ${filename%.mkv}.mp4`"

are somewhat ambiguous, as in, fact, you are echoing - if there is a space in $filename - two chains of characters -. It should be corrected in :

"${filename/%.mkv/.mp4}"
于 2013-01-31T18:37:01.553 回答