Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

bash - Youtube-dl - Extract metadata/json information to text file

Im trying to use jq to extract information from Youtube json information with youtube-dl and format the output to look good.

Example with output

youtube-dl -j https://www.youtube.com/watch?v=TWeRJdrpxEU | jq ".title, .uploader, .webpage_url"

"Cruising – Vendredi (No Copyright Music)"
"Audio Library — Music for content creators"
"https://www.youtube.com/watch?v=TWeRJdrpxEU"

I do not like the " in the output but if I ignore them and try to add extra lines with echo this it dont work and give error below. Youtube-dl work fine in other use cases for me.

youtube-dl -j https://www.youtube.com/watch?v=TWeRJdrpxEU | echo TITLE | jq ".title" | echo CHANNEL | jq ".uploader"

parse error: Invalid numeric literal at line 2, column 0
parse error: Invalid numeric literal at line 2, column 0
Traceback (most recent call last):
  File "/usr/local/bin/youtube-dl", line 33, in <module>
    sys.exit(load_entry_point('youtube-dl==2021.1.8', 'console_scripts', 'youtube-dl')())
  File "/usr/local/Cellar/youtube-dl/2021.1.8/libexec/lib/python3.9/site-packages/youtube_dl/__init__.py", line 474, in main
    _real_main(argv)
  File "/usr/local/Cellar/youtube-dl/2021.1.8/libexec/lib/python3.9/site-packages/youtube_dl/__init__.py", line 464, in _real_main
    retcode = ydl.download(all_urls)
  File "/usr/local/Cellar/youtube-dl/2021.1.8/libexec/lib/python3.9/site-packages/youtube_dl/YoutubeDL.py", line 2028, in download
    res = self.extract_info(
  File "/usr/local/Cellar/youtube-dl/2021.1.8/libexec/lib/python3.9/site-packages/youtube_dl/YoutubeDL.py", line 796, in extract_info
    return self.__extract_info(url, ie, download, extra_info, process)
  File "/usr/local/Cellar/youtube-dl/2021.1.8/libexec/lib/python3.9/site-packages/youtube_dl/YoutubeDL.py", line 803, in wrapper
    return func(self, *args, **kwargs)
  File "/usr/local/Cellar/youtube-dl/2021.1.8/libexec/lib/python3.9/site-packages/youtube_dl/YoutubeDL.py", line 835, in __extract_info
    return self.process_ie_result(ie_result, download, extra_info)
  File "/usr/local/Cellar/youtube-dl/2021.1.8/libexec/lib/python3.9/site-packages/youtube_dl/YoutubeDL.py", line 869, in process_ie_result
    return self.process_video_result(ie_result, download=download)
  File "/usr/local/Cellar/youtube-dl/2021.1.8/libexec/lib/python3.9/site-packages/youtube_dl/YoutubeDL.py", line 1654, in process_video_result
    self.process_info(new_info)
  File "/usr/local/Cellar/youtube-dl/2021.1.8/libexec/lib/python3.9/site-packages/youtube_dl/YoutubeDL.py", line 1764, in process_info
    self.__forced_printings(info_dict, filename, incomplete=False)
  File "/usr/local/Cellar/youtube-dl/2021.1.8/libexec/lib/python3.9/site-packages/youtube_dl/YoutubeDL.py", line 1736, in __forced_printings
    self.to_stdout(json.dumps(info_dict))
  File "/usr/local/Cellar/youtube-dl/2021.1.8/libexec/lib/python3.9/site-packages/youtube_dl/YoutubeDL.py", line 517, in to_stdout
    self._write_string(output, self._screen_file)
  File "/usr/local/Cellar/youtube-dl/2021.1.8/libexec/lib/python3.9/site-packages/youtube_dl/YoutubeDL.py", line 506, in _write_string
    write_string(s, out=out, encoding=self.params.get('encoding'))
  File "/usr/local/Cellar/youtube-dl/2021.1.8/libexec/lib/python3.9/site-packages/youtube_dl/utils.py", line 3180, in write_string
    out.buffer.write(byt)
BrokenPipeError: [Errno 32] Broken pipe

This is the output style I'm after.

- TITLE -
Cruising – Vendredi (No Copyright Music)

- CHANNEL -
Audio Library — Music for content creators

- CHANNEL URL -
https://www.youtube.com/watch?v=TWeRJdrpxEU
question from:https://stackoverflow.com/questions/65648221/youtube-dl-extract-metadata-json-information-to-text-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To emit the strings without quotation marks, simply use the -r command-line option.

To add the headers, you could modify your filter as follows:

"- TITLE -", .title,
"- CHANNEL -", .uploader,
"- CHANNEL URL -", .webpage_url

(It would probably be simpler to put this in a file rather than trying to squeeze it (with all the necessary escapes) on the command line.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...