| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Video::M3U8; |
|
2
|
1
|
|
|
1
|
|
27917
|
use warnings; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
42
|
|
|
3
|
1
|
|
|
1
|
|
6
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
35
|
|
|
4
|
1
|
|
|
1
|
|
3312
|
use WWW::Mechanize; |
|
|
1
|
|
|
|
|
290802
|
|
|
|
1
|
|
|
|
|
424
|
|
|
5
|
|
|
|
|
|
|
my $VERSION = '0.0.3'; |
|
6
|
|
|
|
|
|
|
my $cache = { |
|
7
|
|
|
|
|
|
|
manifest => [], #array of manifest file |
|
8
|
|
|
|
|
|
|
tags => [], #array of all tags |
|
9
|
|
|
|
|
|
|
}; |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub new { |
|
12
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
|
13
|
0
|
|
|
|
|
|
my $self = {}; |
|
14
|
0
|
|
|
|
|
|
my ($manifest) = @_; |
|
15
|
0
|
0
|
|
|
|
|
if ( !defined $manifest ) { die 'Manifest not provided' }; |
|
|
0
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
#check if m3u8 is a file or uri |
|
18
|
0
|
0
|
|
|
|
|
if ($manifest =~ m/http|https/) { |
|
19
|
0
|
|
|
|
|
|
my $mech = WWW::Mechanize->new( agent => 'Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20120716 Firefox/15.0a2' ); |
|
20
|
0
|
|
|
|
|
|
$mech->get( $manifest ); |
|
21
|
0
|
|
|
|
|
|
for my $l ( split( /\n/, $mech->content ) ) { |
|
22
|
0
|
|
|
|
|
|
&process_line( $l ); |
|
23
|
|
|
|
|
|
|
}; |
|
24
|
0
|
|
|
|
|
|
undef $mech; |
|
25
|
0
|
|
|
|
|
|
undef $manifest; |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
} else { |
|
28
|
0
|
|
|
|
|
|
open my $FH, "<", $manifest; |
|
29
|
0
|
|
|
|
|
|
while ( <$FH> ) { |
|
30
|
0
|
|
|
|
|
|
&process_line( $_ ); |
|
31
|
|
|
|
|
|
|
}; |
|
32
|
|
|
|
|
|
|
}; |
|
33
|
|
|
|
|
|
|
|
|
34
|
0
|
|
|
|
|
|
bless $self, $class; |
|
35
|
|
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
|
return $self; |
|
37
|
|
|
|
|
|
|
}; |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub process_line { |
|
40
|
0
|
|
|
0
|
0
|
|
my $line = shift; |
|
41
|
0
|
|
|
|
|
|
push @{ @$cache{ manifest } }, $line; |
|
|
0
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
|
43
|
0
|
0
|
|
|
|
|
if ( $line =~ m/#EXT/ ) { |
|
44
|
0
|
|
|
|
|
|
push @{ @$cache{ tags } }, $line; |
|
|
0
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
}; |
|
46
|
0
|
0
|
|
|
|
|
if ( $line =~ /EXT-X-TARGETDURATION:(.*?)(\n)/ ) { |
|
47
|
0
|
|
|
|
|
|
$cache->{ targetduration } = $1; |
|
48
|
|
|
|
|
|
|
}; |
|
49
|
0
|
0
|
|
|
|
|
if ( $line =~ /EXT-X-ENDLIST/ ) { |
|
50
|
0
|
|
|
|
|
|
$cache->{ end } = 1; |
|
51
|
|
|
|
|
|
|
}; |
|
52
|
|
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
|
return 1; |
|
54
|
|
|
|
|
|
|
}; |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub get_playlist { |
|
57
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
|
58
|
|
|
|
|
|
|
|
|
59
|
0
|
|
|
|
|
|
return @{ @$cache{ manifest } } ; |
|
|
0
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
}; |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub get_tags { |
|
63
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
|
64
|
|
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
|
return @{ @$cache{ tags } }; |
|
|
0
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
}; |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub get_target_duration { |
|
69
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
|
70
|
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
|
return $cache->{ targetduration }; |
|
72
|
|
|
|
|
|
|
}; |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub is_last_playlist { |
|
75
|
|
|
|
|
|
|
|
|
76
|
0
|
|
|
0
|
0
|
|
return $cache->{ end }; |
|
77
|
|
|
|
|
|
|
}; |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head1 Overview |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Module for fetching and working with M3U8 manifest files. |
|
83
|
|
|
|
|
|
|
This is for individual manifests not multi-variant playlists (I may eventually create a module that builds on this) |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=cut |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head2 new |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
$m = Video::M3U8->new($url_or_filename) |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
If the string contains 'http', new() will fetch the contains from the web, if it does not, new will read from the file |
|
92
|
|
|
|
|
|
|
and process the contents, the contents are read into a hash once. since its possible for playlists to be updated consistently, |
|
93
|
|
|
|
|
|
|
the only time it is fetched is on 'new()', all other functions work from cache, each new manifest or seeking updates should |
|
94
|
|
|
|
|
|
|
be called again. |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=cut |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head2 Get playlist |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
Function will return the entire manifest/playlist |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
$m->get_playlist |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=cut |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head2 Get tags |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
If you would just like to use the tags; |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
$m->get_tags |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=cut |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head2 Checking for the last playlist |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
If its an Ondemand playlist or the last in a Live stream (contains EXT-X-ENDLIST tag) this sub will return '1' (true) |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
$m->is_last_playlist |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=cut |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head2 Checking for target duration |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Function will return the target duration |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
$m->get_target_duration |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=cut |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head1 AUTHORS |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Copyright (c) 2012 |
|
133
|
|
|
|
|
|
|
Cost: tell me about bugs or points to improve - have fun. |
|
134
|
|
|
|
|
|
|
Omar Salix, |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=cut |