如何在 Bash 中将变量放入字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14843362/
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
How to put a variable to a string in Bash
提问by user1704620
I am writing a Bash script, and I am trying to figure out a way to get FFmpeg to recognize a global variable in the -force_key_framesoption. The -force_key_framesoption can take a regular expression as an argument, allowing functionality such as forcing a key frame every 5 seconds:
我正在编写一个 Bash 脚本,我试图找出一种方法让 FFmpeg 识别-force_key_frames选项中的全局变量。该-force_key_frames选项可以将正则表达式作为参数,允许每 5 秒强制执行一个关键帧等功能:
-force_key_frames 'expr:gte(t,n_forced*5)'
This works fine for forcing a key frame every 5 seconds, but I don't know how to force a key frame every x seconds, x being an input variable from the user gotten by the rest of the script. The exact FFmpeg command that I'm trying is:
这适用于每 5 秒强制一个关键帧,但我不知道如何每 x 秒强制一个关键帧,x 是用户从脚本的其余部分获取的输入变量。我正在尝试的确切 FFmpeg 命令是:
ffmpeg -i "video.mp4" -vcodec: libx264 -b:v 500k \
-force_key_frames 'expr:gte(t,n_forced*${SEG_TIME})' -s:v 640x480 \
-r 29.97 -pix_fmt yuv420p -map 0 -f segment -segment_time ${SEG_TIME} \
-reset_timestamps 1 -y "output%01d.mp4"
The variable $SEG_TIMEis set to 5 by the script, but the regular expression in the -force_key_framesoption doesn't seem to like the $SEG_TIMEvariable.
$SEG_TIME脚本将变量设置为 5,但-force_key_frames选项中的正则表达式似乎不喜欢该$SEG_TIME变量。
回答by Steven Penny
This part
这部分
'expr:gte(t,n_forced*${SEG_TIME})'
your single quotes are causing the string ${SEG_TIME}to be passed literally rather than interpreted as a variable, try this
你的单引号导致字符串${SEG_TIME}按字面传递而不是解释为变量,试试这个
"expr:gte(t,n_forced*${SEG_TIME})"

