pot*_*day 5 bash zenity sed awk youtube-dl
如何将 youtube-dl 文件下载进度百分比添加到 zenity 进度条
示例代码(只是一个例子,不是一个有效的)
#!/bin/sh
(
progress=$(youtube-dl --extract-audio --audio-quality 0 --newline --audio-format mp3 https://www.youtube.com/playlist?list=PL1C815DB73EC2678E)
per=$(awk '{print perc}' <<<$progress)
time=$(awk '{print time}' <<<$progress)
file_no=$(awk '{print file_no}' <<<$progress) #only for playlist, example=Downloading video 1 of 4
echo "$per" ; sleep 1
echo "# $file_no \n Time Left: $time" ; sleep 1
) |
zenity --progress \
--title="Download" \
--text="Downloading..." \
--percentage=0
if [ "$?" = -1 ] ; then
zenity --error \
--text="Download cancelled."
fi
Run Code Online (Sandbox Code Playgroud)
我已使用此代码获取下载进度
youtube-dl --extract-audio --audio-quality 0 --newline --audio-format mp3 https://www.youtube.com/playlist?list=PL1C815DB73EC2678E
Run Code Online (Sandbox Code Playgroud)
这是输出
[youtube:playlist] PL1C815DB73EC2678E: Downloading webpage
[download] Downloading playlist: Less than 1 minute
[youtube:playlist] playlist Less than 1 minute: Collected 4 video ids (downloading 4 of them)
[download] Downloading video 1 of 4
[youtube] KNLwsqzFfNg: Downloading webpage
[youtube] KNLwsqzFfNg: Extracting video information
[youtube] KNLwsqzFfNg: Downloading DASH manifest
download] Destination: _1 min. - Amendes pour les particules du LHC-KNLwsqzFfNg.m4a
[download] 0.4% of 231.51KiB at 6.10KiB/s ETA 00:30
[download] 1.1% of 231.51KiB at 27.07KiB/s ETA 00:10
[download] 4.0% of 231.51KiB at 19.24KiB/s ETA 00:04
[download] 6.5% of 231.51KiB at 75.06KiB/s ETA 00:03
[download] 13.4% of 231.51KiB at 98.22KiB/s ETA 00:03
[download] 28.7% of 231.51KiB at 81.40KiB/s ETA 00:02
[download] 61.7% of 231.51KiB at 91.56KiB/s ETA 00:01
[download] 86.2% of 231.51KiB at 82.96KiB/s ETA 00:00
[download] 100.0% of 231.51KiB at 73.21KiB/s ETA 00:00
[download] 100% of 231.51KiB in 00:02
[ffmpeg] Correcting container in "_1 min. - Amendes pour les particules du LHC-KNLwsqzFfNg.m4a"
WARNING: Your copy of avconv is outdated, update avconv to version 10-0 or newer if you encounter any errors.
[avconv] Destination: _1 min. - Amendes pour les particules du LHC-KNLwsqzFfNg.mp3
WARNING: Your copy of avconv is outdated, update avconv to version 10-0 or newer if you encounter any errors.
Deleting original file _1 min. - Amendes pour les particules du LHC-KNLwsqzFfNg.m4a (pass -k to keep)
[download] Downloading video 2 of 4
[youtube] wTvXkMpJflk: Downloading webpage
[youtube] wTvXkMpJflk: Extracting video information
[youtube] wTvXkMpJflk: Downloading DASH manifest
etc..
etc..
.
.
Run Code Online (Sandbox Code Playgroud)
我只想要
Downloading video 1 of 4 [download] Downloading video 2 of 4
Run Code Online (Sandbox Code Playgroud)
作为 $files_no
第一个文件
file_no= Downloading video 1 of 4
per time rate
0.40% 00:30:00 6.10KiB/s
1.10% 00:10:00 27.07KiB/s
4.00% 00:04:00 19.24KiB/s
6.50% 00:03:00 75.06KiB/s
13.40% 00:03:00 98.22KiB/s
28.70% 00:02:00 81.40KiB/s
61.70% 00:01:00 91.56KiB/s
86.20% 00:00:00 82.96KiB/s
100.00% 00:00:00 231.51KiB/s
Run Code Online (Sandbox Code Playgroud)
第二,第三...文件
As separate variable $file, $per, $time
i know we can use awk
but for this complicated output how should i use it. if all parameters are not possible, can at least percentage and file_no be extracted.
对的,这是可能的。你需要
#
在行首打印文件编号。Zenity 将自动更新以#
.开头的行的对话框文本。结合上述内容,并实施一点正则表达式魔术,我们得到:
#!/bin/bash
youtube-dl --extract-audio --audio-quality 0 --newline --audio-format mp3 \
https://www.youtube.com/playlist?list=PL1C815DB73EC2678E |
grep --line-buffered -oP '^\[download\].*?\K([0-9.]+\%|#\d+ of \d)' |
zenity --progress \
--title="Download" \
--text="Downloading..." \
--percentage=0
Run Code Online (Sandbox Code Playgroud)
该--line-buffered
选项grep
立即打印其输出,关闭默认缓冲。这-o
使它只打印行的匹配部分并-P
打开 Perl 兼容正则表达式。
正则表达式有点复杂,让我们分解一下:
^\[download\]
: 匹配以[download]
.开头的行。.*?
: 0 个或更多字符,但这?
使它在尽可能短的匹配处停止。\K
:这基本上是一个回顾,它的意思是“忽略任何匹配的东西”。(...|...)
:|
手段或。因此,将(A|B)
匹配 A 或 B。[0-9.]+\%
: 1 个或多个数字或.
后跟一个%
. 这将打印百分比。#\d+ of \d
: a#
后跟一位或多位数字,of
然后再跟一位或多位数字。这与“Y 的视频 X”行匹配。综合起来,该grep
命令将打印:
#1 of 4
0.1%
0.3%
0.8%
1.7%
3.4%
7.0%
14.0%
28.2%
56.5%
99.5%
100.0%
100%
#2 of 4
0.1%
0.3%
0.8%
1.6%
3.4%
6.9%
13.9%
27.8%
55.8%
[...]
Run Code Online (Sandbox Code Playgroud)
等等,这正是zenity
需要的输出。最后,您可以通过实现从命令行指定多个 URL 的功能来使整个事情更有用:
#!/bin/bash
for url in "$@"
do
youtube-dl --extract-audio --audio-quality 0 --newline --audio-format mp3 \
https://www.youtube.com/playlist?list=PL1C815DB73EC2678E |
grep --line-buffered -oP '^\[download\].*?\K([0-9.]+\%|#\d+ of \d)' |
zenity --progress \
--title="Download" \
--text="Downloading..." \
--percentage=0
done
Run Code Online (Sandbox Code Playgroud)
然后,您可以像这样调用脚本:
myscript.sh "http://url1.com" "http://url2.com" "http://urlN.com
Run Code Online (Sandbox Code Playgroud)