Youtube dl-将元数据/json信息提取到文本文件中



我试图使用jq从Youtube dl的Youtube json信息中提取信息,并将输出格式化为好看的。

输出的示例

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"

我不喜欢";在输出中,但如果我忽略它们并尝试添加带有echo的额外行,这将不起作用,并在下面给出错误。Youtube dl在其他用例中对我来说工作得很好。

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

这就是我追求的输出样式。

- TITLE -
Cruising – Vendredi (No Copyright Music)
- CHANNEL -
Audio Library — Music for content creators
- CHANNEL URL -
https://www.youtube.com/watch?v=TWeRJdrpxEU
要发出不带引号的字符串,只需使用-r命令行选项。

要添加标题,您可以如下修改过滤器:

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

(把它放在一个文件中可能会更简单,而不是试图在命令行上压缩它(使用所有必要的转义(。(

相关内容

  • 没有找到相关文章