0

It's about how to get a list of URLs by using youtube_dl. Although I have been trying all day long, I couldn't work it out. Thus I would like to ask help to translate the following command lines (partially in Linux) into Python codes. I mean in a .py file.

  1. To get JSON data, use command line: youtube-dl -j --flat-playlist 'https://www.youtube.com/c/3blue1brown/videos'

  2. To parser use command line in Linux: youtube-dl -j --flat-playlist 'https://www.youtube.com/c/3blue1brown/videos' | jq -r '.id' | sed 's_^_https://youtube.com/v/_'

The codes above are from: https://web.archive.org/web/20180309061900/https://archive.zhimingwang.org/blog/2014-11-05-list-youtube-playlist-with-youtube-dl.html (The youtube link there was removed so I replaced the youtube link above)

1 Answers1

0

You can use the same command to run inside a .py file using os as follows:

import os
os.system("youtube-dl -j --flat-playlist 'https://www.youtube.com/c/3blue1brown/videos'")

You can pipe the output of the above commands to a file and then process your json file in python.

  • It's great! Thanks! –  Nov 29 '20 at 17:08
  • I just aware that the output of the os.system can only be seen on the console. Is there any methods to use the outputs direct in python? I would like to use the outputs for further codes. Thanks. –  Dec 12 '20 at 16:41
  • Maybe take a look here: https://stackoverflow.com/questions/3503879/assign-output-of-os-system-to-a-variable-and-prevent-it-from-being-displayed-on – Mayank Singhal Dec 14 '20 at 04:58
  • Or you can output the result of youtube-dl command to a file and then read from that file. e.g., `os.system("youtube-dl -j --flat-playlist 'https://www.youtube.com/c/3blue1brown/videos' > tempfile.txt")` – Mayank Singhal Dec 14 '20 at 05:00