首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对imagemagick-convert的Bash脚本调用失败,但是语法在粘贴到控制台时有效

对imagemagick-convert的Bash脚本调用失败,但是语法在粘贴到控制台时有效
EN

Stack Overflow用户
提问于 2015-12-05 06:36:13
回答 2查看 805关注 0票数 1

好的测试内容总是很难获得,所以我经常不得不自己制作。

我正在解决隐藏字幕/字幕不能与口语对齐显示的问题。因此,在来回的场景中,字幕似乎明显地错位了。

所以我的计划是使用一个毫秒的“精确”计数器的视频,然后在视频上显示定时文本图像(PNG),通过我没有源代码的应用程序。如果我的测试证明存在时间问题,那么我将能够向供应商索要源代码和/或让他们解决他们的时间问题。

因此,在背景中使用毫秒级的“精确”计数器电影,我打算显示imagemagick-convert创建的PNG,它将具有足够的嵌入文本,以允许高速摄像机在解码/渲染期间从定时文本源文件(场景艺术家DVD SST格式)显示任何未对齐。

我遇到的问题是,我对'convert‘的调用在控制台上可以工作,但在我的bash脚本中不能工作。我已经在单引号和双引号中放置了所有必要的换行符,并尝试通过'xargs‘传递参数,但同样失败了。似乎bash对字符串的解释是在传递给'convert‘之前被解释的。在脚本顶部启用'set -x‘后,我可以看到传递的字符串,有趣的是,如果将该字符串粘贴到控制台,它就可以正常工作。只是它不能直接从脚本中工作。

请告知我的方法是否不正确和/或我是否缺少参数/选项来使其正常工作。

以下是SST文件的几行内容以供参考:

代码语言:javascript
复制
1613    02:29:08:20 02:29:09:14 NightAtTheMuseum_Th_16x9_EnS_DYN_B5-01608_FOX1595.png
1614    02:29:56:10 02:29:57:15 NightAtTheMuseum_Th_16x9_EnS_DYN_B5-01608_FOX1596.png
1615    02:29:57:20 02:29:59:05 NightAtTheMuseum_Th_16x9_EnS_DYN_B5-01608_FOX1597.png

格式:枚举、显示-开始时间hh:mm:ss:帧显示-停止时间hh:mm:ss:帧和图像文件名。

注意:我手动删除了SST文件头,只为熟悉该格式的用户保留了timed-text信息。

Bash-script:

代码语言:javascript
复制
#! /bin/sh
set -x

echo "SST File name passed in <$1>"  

COUNT=0
GFX_FILENAME="test"

while IFS= read -r LINE; do
    COUNT=`expr $COUNT + 1`; # Debug not needed
    echo "Converting entry <$COUNT>... " # Debug not needed

    echo "Line read is <$LINE>" # Debug not needed

    GFX_FILENAME=$(printf "%s" "$LINE" | cut -f4 -d'    ') # works
    GFX_INLINE_TEXT=$(printf "%s" "$LINE" | tr \\t ' ') # works

    GFX_FILENAME="gfx_filename_here.png" # debug, put in place to isolate any issue introduced from parsed value
    GFX_INLINE_TEXT="gfx text here" # debug, put in place to isolate any issue introduced from parsed value

    echo "Filename parsed from line is <$GFX_FILENAME>" # debug to check value before calling convert

    # Method 1, call convert directly:    
    convert -size 720x480 xc:transparent -font Palatino-Bold -pointsize 72 -fill black -draw \"text 20,55 \'$GFX_INLINE_TEXT\'\" $GFX_FILENAME

    # Method 2, echo arguments to xargs then call convert (same result) commented out for now.  Note:  xargs hides 'convert's error output.
#   echo -size 720x480 xc:transparent -font Palatino-Bold -pointsize 72 -fill black -draw \"text 20,55 \'$GFX_INLINE_TEXT\'\" $GFX_FILENAME | xargs

    echo "Converted an image..." # debug, when i find this in console output from script execution I know what's above (from run with set -x enabled) is what I can paste to the console to double-check my syntax

done < $1 # method used to pass file to while loop

echo "Done!!!" # not true, since it doesn't work

输出:

代码语言:javascript
复制
./sst_to_png.sh NightAtTheMuseum_Th_16x9_EnS_DYN_B5-01608_v05.sst
+ echo File name passed in is <NightAtTheMuseum_Th_16x9_EnS_DYN_B5-01608_v05.sst>
File name passed in is <NightAtTheMuseum_Th_16x9_EnS_DYN_B5-01608_v05.sst>
+ COUNT=0
+ GFX_FILENAME=test
+ IFS= read -r LINE
+ expr 0 + 1
+ COUNT=1
+ echo Converting entry <1>... 
Converting entry <1>... 
> echo Line read is <0003   01:01:11:22 01:01:14:25 NightAtTheMuseum_Th_16x9_EnS_DYN_B5-01608_FOX0000.png
>ine read is <0003  01:01:11:22 01:01:14:25 NightAtTheMuseum_Th_16x9_EnS_DYN_B5-01608_FOX0000.png
+ printf %s 0003    01:01:11:22 01:01:14:25 NightAtTheMuseum_Th_16x9_EnS_DYN_B5-01608_FOX0000.png
+ cut -f4 -d    
+ GFX_FILENAME=NightAtTheMuseum_Th_16x9_EnS_DYN_B5-01608_FOX0000.png
+ printf %s 0003    01:01:11:22 01:01:14:25 NightAtTheMuseum_Th_16x9_EnS_DYN_B5-01608_FOX0000.png
+ tr \t  
+ GFX_INLINE_TEXT=0003 01:01:11:22 01:01:14:25 NightAtTheMuseum_Th_16x9_EnS_DYN_B5-01608_FOX0000.png
+ GFX_FILENAME=gfx_filename_here.png
+ GFX_INLINE_TEXT=gfx text here
+ echo Filename parsed from line is <gfx_filename_here.png>
Filename parsed from line is <gfx_filename_here.png>
+ convert -size 720x480 xc:transparent -font Palatino-Bold -pointsize 72 -fill black -draw "text 20,55 'gfx text here'" gfx_filename_here.png
convert.im6: non-conforming drawing primitive definition `text' @ error/draw.c/DrawImage/3158.
convert.im6: unable to open image `20,55': No such file or directory @ error/blob.c/OpenBlob/2638.
convert.im6: no decode delegate for this image format `20,55' @ error/constitute.c/ReadImage/544.
convert.im6: unable to open image `'gfx': No such file or directory @ error/blob.c/OpenBlob/2638.
convert.im6: no decode delegate for this image format `'gfx' @ error/constitute.c/ReadImage/544.
convert.im6: unable to open image `text': No such file or directory @ error/blob.c/OpenBlob/2638.
convert.im6: no decode delegate for this image format `text' @ error/constitute.c/ReadImage/544.
convert.im6: unable to open image `here'"': No such file or directory @ error/blob.c/OpenBlob/2638.
convert.im6: no decode delegate for this image format `here'"' @ error/constitute.c/ReadImage/544.
convert.im6: non-conforming drawing primitive definition `text' @ error/draw.c/DrawImage/3158.
+ echo Converted an image...
Converted an image...

然后我粘贴:

代码语言:javascript
复制
convert -size 720x480 xc:transparent -font Palatino-Bold -pointsize 72 -fill black -draw "text 20,55 'gfx text here'" gfx_filename_here.png

从脚本的输出复制到控制台,它就可以工作了。

提前感谢,希望“转换”错误对某些人有意义。让我感到奇怪的是,在脚本顶部启用了set -x的详细输出,在粘贴到控制台时会产生一些可以工作的内容,但在直接从脚本中执行时却不会产生效果。

EN

回答 2

Stack Overflow用户

发布于 2015-12-05 10:42:35

脚本中的正确行很简单:

代码语言:javascript
复制
convert -size 720x480 xc:transparent -font Palatino-Bold -pointsize 72 -fill black -draw "text 20,55 '$GFX_INLINE_TEXT'" "$GFX_FILENAME"

我在$GFX_FILENAME两边添加了引号,以防文件名包含宽字符。

您不能转义控制台命令行中的引号。为什么需要在脚本中这样做?

票数 1
EN

Stack Overflow用户

发布于 2015-12-09 02:36:46

根本原因答案:

在尝试通过convert使用脏变量之前,需要清理这些变量。所有在引号中使用换行符的尝试都是徒劳的。变量的尾部\r使引号消失。中断将它们放回原处,但会导致convert报告语法中的许多错误。最终,当将变量作为参数传递时,尤其是当字符串中有其他后续参数时,需要“干净”的变量。

我尝试了在以下位置找到的语法:

How to remove carriage return from a string in Bash

但是我不能得到:

代码语言:javascript
复制
echo "${GFX_INLINE_TEXT//[$'\t\r\n ']}"

./sst_to_png.sh: 36:./sst_to_png.sh:替换错误

为我工作。

所以我说:

代码语言:javascript
复制
GFX_INLINE_TEXT=$(printf "%s" "$LINE" | tr \\t ' ' | tr -d '\n' | tr -d '\r')

这使我可以用制表符替换空格,然后在将变量作为参数传递给转换之前,从变量中删除尾随的\r (返回)实例。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34098640

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档