HandBrakeCLI bash 脚本转换文件夹中的所有视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19562785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:23:50  来源:igfitidea点击:

HandBrakeCLI bash script convert all videos in a folder

bashvideohandbrake

提问by John Roe

Firstly, I searched around for my problem. But none can solve it.

首先,我四处寻找我的问题。但没有人能解决它。

I want to convert all videos file in a directory and the output will be saved in another directory. I got a bash script from somewhere I dont remember.

我想转换一个目录中的所有视频文件,输出将保存在另一个目录中。我从某个我不记得的地方得到了一个 bash 脚本。

#!/bin/bash

SRC="/home/abc/public_html/filex/store/vids/toriko/VIDEOS HERE"
DEST="/home/abc/public_html/filex/store/vids/toriko/51-100"
DEST_EXT=mp4
HANDBRAKE_CLI=HandBrakeCLI
PRESET="iPhone & iPod Touch"

for FILE in "`ls $SRC`"
do
filename=$(basename $FILE)
extension=${filename##*.}
filename=${filename%.*}

$HANDBRAKE_CLI -i "$SRC"/$FILE -o "$DEST"/"$filename".$DEST_EXT "$PRESET"

done

the problem is, the output of the file will be without filename.. only ".mp4". and, there is only 1 file generated.. means, from 50 videos in the folder, only 1 files generated with name ".mp4" and after that, HandBrakeCLI exit. can anyone fix my code? I got no experince in bash coding.. so, the right script giiven will be appreciate :)

问题是,文件的输出将没有文件名......只有“.mp4”。并且,只生成了 1 个文件.. 意味着,从文件夹中的 50 个视频中,只生成了 1 个名为“.mp4”的文件,之后,HandBrakeCLI 退出。谁能修复我的代码?我在 bash 编码方面没有经验..所以,正确的脚本将不胜感激:)

采纳答案by damienfrancois

Your line

你的线路

for FILE in "`ls $SRC`"

effectively creates only one iteration where FILEcontains the list of the files (and it is not able to handle the space in $SRC). Better replace it with

有效地仅创建一个FILE包含文件列表的迭代(并且它无法处理 中的空间$SRC)。最好换成

for FILE in "$SRC"/*

Example:

例子:

$ ls test
1.txt  2.txt
$ SRC=test; for f in "`ls $SRC`" ; do echo $f; done
1.txt 2.txt
$ SRC=test; for f in "$SRC"/* ; do echo $f; done
test/1.txt
test/2.txt

Side note: you can have a space in there with no problem

旁注:你可以在那里有一个空间没有问题

$ ls "the test"
1.txt  2.txt
$ SRC="the test"; for f in "$SRC"/* ; do echo $f; done
the test/1.txt
the test/2.txt

回答by FreeSoftwareServers

I tried this script, and others like it, but I wanted to convert recursive directory tree's and have files placed in the same directory with .mp4 extension and delete .avi files, after much trial and error I gave up on this code and searched for a new code, id like to credit

我试过这个脚本,还有其他人喜欢它,但我想转换递归目录树,并将文件放在同一个目录中,扩展名为 .mp4 并删除 .avi 文件,经过多次试验和错误,我放弃了这段代码并搜索一个新代码,我喜欢信用

http://www.surlyjake.com/blog/2010/08/10/script-to-run-handbrake-recursively-through-a-folder-tree/

http://www.surlyjake.com/blog/2010/08/10/script-to-run-handbrake-recursively-through-a-folder-tree/

For the original code!

为原码!

Here is my modified script, barely modified BTW this script is short, sweet and easy to understand.

这是我修改后的脚本,几乎没有修改 顺便说一句,这个脚本简短,甜美且易于理解。

#!/bin/bash

# This Script Goes in Root Folder of TV show -- Example Folder Structure
# /Stargate/Season\ 1/Epiosde.avi
# /Stargate/Season\ 2/Epiosde.avi
# /Stargate/handbrake_folder.script
# Outputs all Files back inside same dir's and does all folders inside Startgate DIR
# /Stargate/Season\ 1/Epiosde.mp4
# /Stargate/Season\ 2/Epiosde.mp4

# PRESET = -o flags for CLI can be got from GUI under Activity Log or from https://trac.handbrake.fr/wiki/CLIGuide OR you can use actual Presets!

# PRESET="iPhone & iPod Touch"

PRESET="--modulus 2 -e x264 -q 20 --vfr -a 1 -E ac3 -6 5point1 -R Auto -B 384 -D 0 --gain 0 --audio-fallback ac3 --encoder-preset=veryfast  --encoder-level="5.2"  --encoder-profile=high  --verbose=1"

if [ -z "" ] ; then
TRANSCODEDIR="."
else
TRANSCODEDIR=""
fi
find "$TRANSCODEDIR"/* -type f -name "*.avi" -exec bash -c 'HandBrakeCLI -i "" -o "${1%\.*}".mp4 --preset="$PRESET"' __ {} \; && find . -name '*.avi' -exec rm -r     {} \;

BE WARNED: THIS WILL CONVERT THEN DELETE ALL .AVI FILES ABOVE THE SCRIPT IN FILE TREE!

警告:这将转换然后删除文件树中脚本上方的所有 .AVI 文件!

Feel free to remove the

随意删除

[-name "*.avi"] & [&& find . -name '*.avi' -exec rm -r     {} \;]

to disable only converting .avi and removal of .avi or modify to suite another extension.

禁用仅转换 .avi 和删除 .avi 或修改以适应另一个扩展。

Cheers and hope this helps somebody!

欢呼并希望这对某人有所帮助!

回答by Joakim Kartveit

I have found the solution:

我找到了解决方案:

#!/bin/bash

SRC="/home/abc/public_html/filex/store/vids/toriko/VIDEOS HERE"
DEST="/home/abc/public_html/filex/store/vids/toriko/51-100"
DEST_EXT=mp4
HANDBRAKE_CLI=HandBrakeCLI

for FILE in "$SRC"/*
do
    filename=$(basename "$FILE")
    extension=${filename##*.}
    filename=${filename%.*}
    $HANDBRAKE_CLI -i "$FILE" -o "$DEST"/"$filename".$DEST_EXT
done

回答by Roger

I'd rather prefer this solution:

我更喜欢这个解决方案:

#!/bin/bash

SRC=""
DEST=""
EXT='mp4'
PRESET='iPhone & iPod Touch'

#for FILE in "`ls $SRC`"; do
for FILE in `find . -type f`; do
    FILE=$(basename "$FILE")
    filename=$(basename "$FILE")
    extension=${filename##*.}
    filename=${filename%.*}

    HandBrakeCLI -i "$SRC"/$FILE -o "$DEST"/"$filename"."$EXT" "$PRESET"
done

回答by mmcc73

I just tried using this script with the modification suggested above. I found I need to to put double quotes around the two uses of $FILE in order to handle file names with spaces.

我只是尝试使用上面建议的修改来使用这个脚本。我发现我需要在 $FILE 的两个用途周围加上双引号,以便处理带空格的文件名。

So...

所以...

filename=$(basename "$FILE")

文件名=$(基本名称“$FILE”)

and

$HANDBRAKE_CLI -i "$SRC"/"$FILE" -o "$DEST"/"$filename".$DEST_EXT "$PRESET"

$HANDBRAKE_CLI -i "$SRC"/"$FILE" -o "$DEST"/"$filename".$DEST_EXT "$PRESET"