簡體   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