-
Notifications
You must be signed in to change notification settings - Fork 3
/
m3ufromyt
executable file
·140 lines (117 loc) · 3.66 KB
/
m3ufromyt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/bin/bash
#copyright 2020 https://github.com/kanliot Apache 2.0 Free to use
helpexit () {
echo "Usage: m3ufromyt ./out.m3u"
echo "Create a nice m3u file from an youtube url."
echo 'a SINGLE youtube url will be prompted for..'
echo "please don't redirect output."
echo "you should enter a youtube url at the prompt."
echo "Youtube urls to a playlist, or to channel/videos have been tested"
echo 'this script is slow on big channels. about 1000 videos per hour'
echo 'option -r allows you to reverse the .m3u playlist'
echo 'option -q allows you to make this program quiet'
exit
}
test $# -eq 0 && helpexit
for a
do test "$a" = '-h' && helpexit
test "$a" = '--help' && helpexit
test "$a" = '-r' && rev=yes && continue
test "$a" = '-q' && quiet=yes && continue
outfile=$a
done
2>/dev/null touch "$outfile" || { echo cannot stat:\'"$outfile"\'; exit 1;}
echo -n enter youtube URL": "
read
perl3 () {
perl -e'
#!/usr/bin/perl
use open ":std", ":utf8";
$| = 1;
sub to_seconds {
my $has_ms = scalar $_[0] =~ /\.\d+/;
my @components = split /[;:\.]/, $_[0];
push @components, 0 if not $has_ms;
@components = reverse @components;
push @components, 0 if $#components < 3; # hours are opt.
push @components, 0 if $#components < 3; # minutes are opt.
# now we should have an array of ms, s, min, h.
return ( ( $components[3] * 60 + $components[2] ) * 60 + $components[1] ) +
".$components[0]";
# return (($components[3] * 60 + $components[2]) * 60 + $components[1]) * 1000 + ".$components[0]" * 1000;
}
sub date {
my $a = shift;
my $month = substr( $a, 4, 2 );
my $day = substr( $a, 6, 2 );
my $year = substr( $a, 0, 4 );
join "/", $month, $day, $year;
}
while (1) {
last if eof STDIN;
my $s = to_seconds( scalar readline STDIN );
$a = date( scalar readline STDIN );
$b = <STDIN>;
$_ = <STDIN>;
y/,/./; # comma is field sep in m3u EXTINF
print "#EXTINF:$s,$a $_", "http://www.youtube.com/watch?v=$b\n";
}
print "#done.\n";
'
}
perl2 () {
perl -e'
#!/usr/bin/perl
use open ":std", ":utf8";
$| = 1;
sub to_seconds {
my $has_ms = scalar $_[0] =~ /\.\d+/;
my @components = split /[;:\.]/, $_[0];
push @components, 0 if not $has_ms;
@components = reverse @components;
push @components, 0 if $#components < 3; # hours are opt.
push @components, 0 if $#components < 3; # minutes are opt.
# now we should have an array of ms, s, min, h.
return ( ( $components[3] * 60 + $components[2] ) * 60 + $components[1] ) +
".$components[0]";
# return (($components[3] * 60 + $components[2]) * 60 + $components[1]) * 1000 + ".$components[0]" * 1000;
}
sub date {
my $a = shift;
my $month = substr( $a, 4, 2 );
my $day = substr( $a, 6, 2 );
my $year = substr( $a, 0, 4 );
join "/", $month, $day, $year;
}
while (1) {
last if eof STDIN;
$_ = <STDIN>;
y/,/./; # comma is field sep in m3u EXTINF
$b = <STDIN>;
$a = date( scalar readline STDIN );
my $s = to_seconds( scalar readline STDIN );
print "#EXTINF:$s,$a $_", "http://www.youtube.com/watch?v=$b\n";
}
print "#done.\n";
'
}
getlines () {
youtube-dl -i --get-id -e -o '%(upload_date)s' --get-filename --get-duration -- "$1"
}
main () {
test "$quiet" && echo -n holdon >&2
test "$quiet" || echo holdon >&2
echo "#EXTM3U"
echo '#' generated from "$@"
echo
if [ "$rev" ]
then getlines "$@"|tac|perl3
else getlines "$@"|perl2
fi
}
if [ ! "$quiet" ]
then main "$REPLY" |tee "$outfile"
else main "$REPLY" > "$outfile"
fi
test $? != 0 && exit 1
echo -e '\rdone.' $(realpath "$outfile")