Search code examples
linuxbashffmpeggrepyoutube-dl

How to repeat a command for every line in a textfile with given arguments from the textfile (Bash?)


I have a .txt file structured like this:

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

I want to repeat for every line this command:

sh download_youtube.sh <youtube's URL> <HH:mm:ss.milisecs from time> <HH:mm:ss.milisecs to time> <output_file_name>

How do i map the url part and the timestamp part of the .txt lines to the command?

I am trying to make a tool https://github.com/moeC137/video-recutter/blob/main/readme.md that downloads all specific instances of a word beeing used on a youtube channel and compiles all instances into a compilation.


Solution

  • Assuming the input file is formatted as a fixed width text, would you please try:

    #!/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"
    

    Save the code above as a file, say mycmd.sh, then execute it with bash mycmd.sh file.txt as a dry run. Note the file.txt is the file which contains the download information as posted. If the printed output looks good, drop echo of the mycmd.sh and execute the actual run.