linux - 如何使用文本文件中的给定参数对文本文件中的每一行重复一个命令(Bash?)

标签 linux bash ffmpeg grep youtube-dl

关闭。这个问题需要更多 focused .它目前不接受答案。












想改进这个问题?更新问题,使其仅关注一个问题 editing this post .


8 个月前关闭。







Improve this question




我有一个结构如下的 .txt 文件:

02.01.2021-EwkqKbhcH5Q.webm.srt-00:00:26,190 --> 00:00:26,670
02.01.2021-EwkqKbhcH5Q.webm.srt-00:00:59,490 --> 00:00:59,880
02.01.2021-EwkqKbhcH5Q.webm.srt-00:03:15,570 --> 00:03:16,230
02.01.2021-EwkqKbhcH5Q.webm.srt-00:03:44,160 --> 00:03:44,730
02.01.2021-EwkqKbhcH5Q.webm.srt-00:04:32,040 --> 00:04:32,370
02.01.2021-EwkqKbhcH5Q.webm.srt-00:04:49,860 --> 00:04:50,250
02.01.2021-EwkqKbhcH5Q.webm.srt-00:05:58,200 --> 00:05:58,620
02.01.2021-EwkqKbhcH5Q.webm.srt-00:08:59,280 --> 00:08:59,760
02.01.2021-EwkqKbhcH5Q.webm.srt-00:10:20,830 --> 00:10:21,340
02.02.2021-9RaIIX8ycHg.webm.srt-00:00:23,820 --> 00:00:24,210
02.02.2021-9RaIIX8ycHg.webm.srt-00:00:40,140 --> 00:00:40,590
02.02.2021-9RaIIX8ycHg.webm.srt-00:13:03,905 --> 00:13:04,295
02.03.2021-FcNGyLY4nuw.webm.srt-00:00:25,680 --> 00:00:26,190
02.03.2021-FcNGyLY4nuw.webm.srt-00:00:44,220 --> 00:00:44,700
我想为每一行重复这个命令:
sh download_youtube.sh <youtube's URL> <HH:mm:ss.milisecs from time> <HH:mm:ss.milisecs to time> <output_file_name>
如何将 .txt 行的 url 部分和时间戳部分映射到命令?
我正在尝试制作一个工具https://github.com/moeC137/video-recutter/blob/main/readme.md它会下载 youtube channel 上使用的单词的所有特定实例,并将所有实例编译成汇编。

最佳答案

假设输入文件被格式化为固定宽度的文本,请您尝试:

#!/bin/bash

infile=$1                                       # input filename
count=1                                         # filename serial number
while IFS= read -r line; do                     # read the input file assigning line
    vid=${line:11:11}                           # video id of youtube
    start=${line:32:12}                         # start time
    stop=${line:49:12}                          # stop time
    file=$(printf "clip%03d.mp4" "$count")      # output filename
    (( count++ ))                               # increment the number
    echo sh download_youtube.sh "youtube.com/watch?v=$vid" "$start" "$stop" "$file"
done < "$infile"
将上面的代码另存为文件,例如 mycmd.sh , 然后用 bash mycmd.sh file.txt 执行作为试运行。注意 file.txt是包含发布的下载信息的文件。如果打印输出看起来不错,请删除 echomycmd.sh并执行实际运行。

关于linux - 如何使用文本文件中的给定参数对文本文件中的每一行重复一个命令(Bash?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69982768/

相关文章:

linux - 如何让服务器每隔几秒获取一个 JSON 文件?

c++ - TCP 服务器不接受客户端发出的正确数量的连接,具有少量监听积压

linux - 为什么 bash 返回负数而不是 2^64?

bash - 使用 bash 脚本为 Sublime Text 2 构建系统

linux - 如何将 head 命令的结果添加到文件的第一行?

ffmpeg - 直接来自 NVENC 编码器的 RTMP 直播流

linux - 如何将多行回显到来自 grep 结果的文件中

linux - 双斜杠//in `cd//` 在 Linux 中是什么意思?

ubuntu - 在视频中添加超链接

video - 需要帮助理解 HTML 5 音频/视频元数据的使用和放置