编辑:检查底部的我的编辑

编辑:检查底部的我的编辑

好的,我编写了一个函数,循环遍历文件夹中的所有文件(仅深度为 1)并将它们压缩为较小的文件大小。

编辑:检查底部的我的编辑

function compressMP4batch()  {
for fname in *.mp4
do

#take off the mp4
pathAndName=${fname%.mp4}

#take off the path from the file name
videoname=${pathAndName##*/}

#take off the file name from the path
videopath=$pathAndName:h

#create new folder for converted icons to be placed in
mkdir -p ${videopath}/compressed/

ffmpeg -y -r 30  -i ${fname} -vcodec libx265 -crf 18 -acodec mp3 -movflags +faststart ${videopath}/compressed/${videoname}-compressed.mp4



echo "\033[1;33m compressed ${videoname}\n \033[0m"
done

}

我目前正在使用 OBS 翻录 VHS 磁带,我希望可以设置一个监视功能,使其在每次添加新文件时运行。

我在其他线程中看到过 inotifywait 可用于此,但我不太明白它的语法是如何设置的等等。

例如我看到此主题这个脚本:

#!/bin/bash
dir=BASH
inotifywait -m "$dir" -e close_write --format '%w%f' |
    while IFS=' ' read -r fname
    do
        [ -f "$fname" ] && chmod +x "$fname"
    done

因此,第一个问题是,如果我只是在其中设置当前功能,那么我认为它最终会在每次添加新文件时尝试压缩所有文件。

因此,我可以取出 for 循环,然后运行 ​​ffmpeg 命令

但是 inotifywait 命令返回什么?它只返回已更改文件的文件名吗?

编辑:好的,所以我仔细考虑了一下,然后我查看了手册页,我想我已经弄明白了,只是我的事件被 OBS 触发了两次,而不是在 OBS 真正完成制作文件时触发

## Requires ffmpeg and inotify-tools

# testing with this: inotifywait -m . -e create  --format '%w%f' | cat

# inotifywait --monitor . --event create --event move_to --event modify  --format '%w%f'

function compressMP4watch()  {
 inotifywait --monitor . --event create  --format '%w%f' |

 while read -r fname
    do

    #take off the mp4
    pathAndName=${fname%.mp4}

    #take off the path from the file name
    videoname=${pathAndName##*/}

    #take off the file name from the path
    videopath=$pathAndName:h

    mkdir -p ${videopath}/compressed/

    ffmpeg -y -r 30  -i ${fname} -vcodec libx265 -crf 18 -acodec mp3 -movflags +faststart ${videopath}/compressed/${videoname}-compressed.mp4

    echo "\033[1;33m compressed ${videoname}\n \033[0m"

    done

}

答案1

这是我的建议:

#!/bin/bash
# dir to whatch
dir=~/your_dir/

# trigger when a file is moved to the dir
inotifywait -m $dir -e moved_to | \
while read -r i; do 
  # get the file name
  file_name=$(echo "$i" | grep -o "MOVED_TO.*" | sed 's/MOVED_TO //')
  # file name plus dir = full path
  file="$dir$filename"
  # apply your function (not the loop) to the file, you can pass the file as parameter
  your_function "$file"
done

我认为有更奇特的方法可以做到这一点。测试一下它是否有效。

相关内容