0

youtube-dl can take some time parsing remote sites when called multiple times.

EDIT0 : I want to fetch multiple properties (here fileNames and remoteFileSizes) output by youtube-dl without having to run it multiple times.

I use those 2 properties to compare the local file size and ${remoteFileSizes[$i]} to tell if the file is finished downloading.

$ youtube-dl --restrict-filenames -o "%(title)s__%(format_id)s__%(id)s.%(ext)s" -f m4a,18,webm,251 -s -j https://www.youtube.com/watch?v=UnZbjvyzteo 2>errors_youtube-dl.log | jq -r ._filename,.filesize | paste - - > input_data.txt
$ cat input_data.txt
Alan_Jackson_-_I_Want_To_Stroll_Over_Heaven_With_You_Live__18__UnZbjvyzteo__youtube_com.mp4 8419513
Alan_Jackson_-_I_Want_To_Stroll_Over_Heaven_With_You_Live__250__UnZbjvyzteo__youtube_com.webm   1528955
Alan_Jackson_-_I_Want_To_Stroll_Over_Heaven_With_You_Live__140__UnZbjvyzteo__youtube_com.m4a    2797366
Alan_Jackson_-_I_Want_To_Stroll_Over_Heaven_With_You_Live__244__UnZbjvyzteo__youtube_com.webm   8171725

I want the first column in the fileNames array and the second column in the remoteFileSizes.

For the time being, I use a while read loop, but when this loop is finished my two arrays are lost :

$ fileNames=()
$ remoteFileSizes=()
$ cat input_data.txt | while read fileName remoteFileSize; do \
    fileNames+=($fileName); \
    remoteFileSizes+=($remoteFileSize); \
done
$ for fileNames in "${fileNames[@]}"; do \
    echo PROCESSING....; \
done
$ echo "=> fileNames[0] = ${fileNames[0]}"
=> fileNames[0] = 
$ echo "=> remoteFileSizes[0] = ${remoteFileSizes[0]}"
=> remoteFileSizes[0] = 
$

Is it possible to assign two bash arrays with a single command ?

3
  • 1
    It's really not clear from this post what is wrong with your current solution or what you are actually wanting Commented Apr 21, 2020 at 20:50
  • @jordanm See my EDIT0 Commented Apr 21, 2020 at 21:04
  • 3
    it's not clear (to me) what any of this has to do with arrays; ok, so we see the sample data you're working with, but we have no idea what you want to do with this data, eg, what part(s) of the data do you want to store in an array(s)? consider updating your question with an additional section showing the expected array structures/contents Commented Apr 21, 2020 at 21:16

2 Answers 2

3

You assign variables in a subshell, so they are not visible in the parent shell. Read https://mywiki.wooledge.org/BashFAQ/024 . Remove the cat and do a redirection to solve your problem.

while IFS=$'\t' read -r fileName remoteFileSize; do
    fileNames+=("$fileName")
    remoteFileSizes+=("$remoteFileSize")
done < input_data.txt

You might also interest yourself in https://mywiki.wooledge.org/BashFAQ/001.

Sign up to request clarification or add additional context in comments.

Comments

2

For what it's worth, if you're looking for specific/bespoke functionality from youtube-dl, I recommend creating your own python scripts using the 'embedded' approach: https://github.com/ytdl-org/youtube-dl/blob/master/README.md#embedding-youtube-dl

You can set your own signal for when a download is finished (text/chime/mail/whatever) and track downloads without having to compare file sizes.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.