繁体   English   中英

重击| 在转义空格时为变量分配参数

[英]bash| assign arguments to variables while escaping spaces

这是bash脚本,它打印视频的帧速率,并且它接受的参数可能包含空格。 该参数也将在脚本内的命令中使用。

#!/bin/bash
inputVid="$*"

#checking if $inputVid has full path 
echo $inputVid

frames=`ffmpeg -i $inputVid 2>&1 | sed -n "s/.*, \(.*\) fp.*/\1/p"`
echo $frames

当我执行

$./frameRate.sh ../Downloads/FlareGet/Videos/Why\ .mp4 

输出为:

../Downloads/FlareGet/Videos/Why .mp4

因此文件名正确传递,但空格未转义,因此ffm​​peg没有输出

有什么办法解决这个问题?

如果您的命令仅接受一个参数,则使用$1 您需要做的就是在脚本中正确地引用原始参数参数$1

# Equivalent invocations
$ ./frameRate.sh ../Downloads/FlareGet/Videos/Why\ .mp4
$ ./frameRate.sh ../Downloads/FlareGet/Videos/"Why .mp4"
$ ./frameRate.sh "../Downloads/FlareGet/Videos/Why .mp4"

该脚本将是

inputVid="$1"
ffmpeg -i "$inputVid" 2>&1 | sed -n "s/.*, \(.*\) fp.*/\1/p"

或简单地

ffmpeg -i "$1" 2>&1 | sed -n "s/.*, \(.*\) fp.*/\1/p"

如果这不起作用,则说明您的Python脚本未正确传递参数,并且您实际上无能为力。

除了在输入变量周围使用双引号之外,还应该使用ffprobe而不是ffmpeg来获取媒体文件信息。 ffmpeg的输出仅供参考,不能通过脚本进行解析: ffmpeg的输出被认为是“易碎的”,不能保证提供标准的,一致的格式。 使用ffprobe还可以消除sed

#!/bin/bash

# Output file path and name
echo "$1"

# Output average frame rate
ffprobe -loglevel error -select_streams v:0 -show_entries stream=avg_frame_rate -of default=noprint_wrappers=1:nokey=1 "$1"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM