使用播放列表文件中的字段并将它们用作命令的参数

使用播放列表文件中的字段并将它们用作命令的参数

播放列表文件的示例内容:

1. The fire is on - 03:50
2. Abc dge khji kkt mmy kdj - 09:20
3. Blowing in the winds  - 14:16
4. By the rivers of Babylon - 15:46
5. Waka waka it's time for africa - 20:30
6. DGF djf Kmf pffg jdkf dhf - 28:25
7. Fdsa djf | kf |- 34:25
8. Despacito despatico - 41:33
...
...
...

命令 -ffmpeg -i "a" -ss "b" -to "c" "output"

现在从列表中,从开头(即从序列号)到文本行末尾(也可能包括管道)的内容应该是命令的最后一个参数(在“输出”的位置) ,末尾的时间戳应该是参数 -ss 的参数,下一行的时间戳应该是参数的参数-to

这与这个非常相似问题但我不太确定如何修改 awk 命令以适应这种特殊情况。

答案1

听起来您正在尝试做的事情是这样的:

$ cat tst.awk
BEGIN {
    fmt = "ffmpeg -i \047a\047 -ss \047%s\047 -to \047%s\047 \047%s\047\n"
}
NR > 1 {
    printf fmt, beg, $NF, title
}
{
    beg = $NF
    title = $0
}
END {
    printf fmt, beg, "forever?", title
}

$ awk -f tst.awk file
ffmpeg -i 'a' -ss '03:50' -to '09:20' '1. The fire is on - 03:50'
ffmpeg -i 'a' -ss '09:20' -to '14:16' '2. Abc dge khji kkt mmy kdj - 09:20'
ffmpeg -i 'a' -ss '14:16' -to '15:46' '3. Blowing in the winds  - 14:16'
ffmpeg -i 'a' -ss '15:46' -to '20:30' '4. By the rivers of Babylon - 15:46'
ffmpeg -i 'a' -ss '20:30' -to '28:25' '5. Waka waka it's time for africa - 20:30'
ffmpeg -i 'a' -ss '28:25' -to '34:25' '6. DGF djf Kmf pffg jdkf dhf - 28:25'
ffmpeg -i 'a' -ss '34:25' -to '41:33' '7. Fdsa djf | kf |- 34:25'
ffmpeg -i 'a' -ss '41:33' -to 'forever?' '8. Despacito despatico - 41:33'

但如果没有看到你的预期输出,这只是一个猜测。

不要在输出中使用单引号而不是双引号 - 这非常重要,这样当您ffmpeg稍后执行命令时,shell 不会首先解释引号内的文本。看https://mywiki.wooledge.org/Quotes

答案2

剧本改编awk其他问题

awk -v input="a" -v to_last="55:55" '
  BEGIN {
    str="ffmpeg -i \"%s\" -ss \"%s\" -to \"%s\" \"%s\"\n"
  }
  NR>1 {
    printf str, input, ss, $NF, output
  }
  {
    ss=$NF                       # last field is the `ss` field
    $NF=""                       # clear last field
    output=$0                    # set the remaining line to output
    sub(/[-| \t]+$/, "", output) # remove blanks, `-` and `|` at the end of output
  }
  END{
    printf str, input, ss, to_last, output
  }
' playlist

...输出(如果从播放列表中删除行):

ffmpeg -i "a" -ss "03:50" -to "09:20" "1. The fire is on"
ffmpeg -i "a" -ss "09:20" -to "14:16" "2. Abc dge khji kkt mmy kdj"
ffmpeg -i "a" -ss "14:16" -to "15:46" "3. Blowing in the winds"
ffmpeg -i "a" -ss "15:46" -to "20:30" "4. By the rivers of Babylon"
ffmpeg -i "a" -ss "20:30" -to "28:25" "5. Waka waka it's time for africa"
ffmpeg -i "a" -ss "28:25" -to "34:25" "6. DGF djf Kmf pffg jdkf dhf"
ffmpeg -i "a" -ss "34:25" -to "41:33" "7. Fdsa djf | kf"
ffmpeg -i "a" -ss "41:33" -to "55:55" "8. Despacito despatico"

相关内容