Search code examples
linuxbashyoutubexargs

xargs with multiple arguments and waiting between certain commands


To preface this, I'm completely new to programming and I'm clueless in general.

Ok, so I made a script that I will now explain.


# Input YouTube video URL into tool that will download the YouTube video file and then uploads it to Internet Archive (archive.org)

tubeup -i $URL

# Wait 15 seconds to avoid any connection errors

sleep 15

# Uses yt-dlp to parse YouTube video URL to download the YouTube video's comment information to a file

yt-dlp --get-comments --no-write-info-json --skip-download --print-to-file "%(comments)#j" "%(id)s.comments" $URL

# Uses yt-dlp to parse YouTube video URL to fetch the YouTube video's information and assigns them to variables

EXTRACTOR=$(yt-dlp --skip-download --print "%(extractor)s" $URL)
ID=$(yt-dlp --skip-download --print "%(id)s" $URL)

# Uploads comment information file to the correct file repository on Internet Archive (archive.org)

ia upload $EXTRACTOR-$ID $ID.comments

# Wait 15 seconds again to avoid any possible errors

sleep 15

# Delete the YouTube video comment information file from my local harddrive

rm $ID.comments

# Reset previously used variables

unset URL
unset EXTRACTOR
unset ID

This script has been very useful for me when I want to upload YouTube videos by manually inputting YouTube URLs. But recently, I want to input a text file of URLs into the script and the script will automatically execute these commands sequentially and one at a time for each video.

It looks like xargs could allow me to do this but I looked at the other threads about xargs and they all made me confused.

Could someone help me?

I tested xargs examples in other threads but they didn't work the way I expected them to.

xargs command I tried to create from examples in: Running multiple commands with xargs:

cat list.txt | xargs -i  sh -c 'tubeup -i {} | sleep 15 && yt-dlp --get-comments --no-write-info-json --skip-download --print-to-file "%(comments)#j" "%(id)s.comments" {} && EXTRACTOR=$(yt-dlp --skip-download --print "%(extractor)s" {}) && ID=$(yt-dlp --skip-download --print "%(id)s" {}) && ia upload $EXTRACTOR-$ID $ID.comments && sleep 15 && rm $ID.comments && unset URL && unset EXTRACTOR && unset ID'

The problem with the code snippet I made is that xargs doesn't wait for the first command to finish until it executes the second command. Like it tries to upload to Internet Archive even though the video hasn't finished downloading yet.


Solution

  • Answered by @jhdc's comment:

    Naming is powerful. If you have a working script that takes a url, just use it:

    <list.txt xargs -I@ path/to/yourscript @