Adding support for video file sizes
[catagits/App-IdiotBox.git] / lib / Utils / PresentingPerl.pm
CommitLineData
1a1d7b64 1package Utils::PresentingPerl;
2
3use strict;
4use warnings;
5
6use FLV::File;
7#use Data::Dump::Streamer;
8use Carp;
9use English '-no_match_vars';
10use IO::Handle;
11
12# Given a filename, and an arrayref of information keys that we are interested in, returns a hashref with those keys filled in.
13# If it cannot determine the information asked for (including if the keys are unknown), it will eventually return a hashref without them filled in.
14# Known tags: 'width', 'height'. Size of video frame. Does not allow for changing size during the video.
15sub get_flv_info {
16 my ($filename, $wanted_info) = @_;
17
18 my $file = FLV::File->new;
19
20 # We need to use a modified version of FLV::File::parse, or we'll end up parsing the whole file.
21 flv_parse($file, $filename);
22
23 #Dump $file;
24
25 my $info;
26
27 # Now, we need to parse out the file, oen bit at at time, until we have what we wanted.
28 # This is from FLV::Body::parse
29 while (1) {
30 my $lastsize = $file->get_bytes(4);
31
32 my $tag = FLV::Tag->new;
33 $tag->parse($file, {});
34 my $payload = $tag->get_payload;
35
36 if ($payload->isa('FLV::VideoTag')) {
37 $info->{width} = $payload->{width};
38 $info->{height} = $payload->{height};
39 } else {
40 # For the time being, assume that other tags are unimportant.
41 # Could, plasuably, tell somthing about the audio... but what do we care about?
42 # Much more plausably, if our file had metadata tags, then we could read them.
43 #die "No handler for payload $payload";
44 }
45
46 for (@$wanted_info) {
47 next if !exists $info->{$_};
48 }
49
50 last;
51 }
52
53 return $info;
54}
55
56sub flv_parse {
57 my $self = shift;
58 my $input = shift;
59 my $opts = shift;
60 $opts ||= {};
61
62 $self->{header} = undef;
63 $self->{body} = undef;
64 $self->{filename} = undef;
65 $self->{filehandle} = undef;
66 $self->{pos} = 0;
67
68 my $eval_result = eval {
69 if (ref $input) {
70 $self->{filehandle} = $input;
71 } else {
72 $self->{filename} = $input;
73 ## no critic (RequireBriefOpen)
74 open my $fh, '<', $self->{filename} or croak q{} . $OS_ERROR;
75 binmode $fh or croak 'Failed to set binary mode on file';
76 $self->{filehandle} = $fh;
77 }
78
79 $self->{header} = FLV::Header->new();
80 $self->{header}->parse($self); # might throw exception
81
82 $self->{body} = FLV::Body->new();
83 # $self->{body}->parse($self, $opts); # might throw exception
84 1;
85 };
86 if (!$eval_result) {
87 die 'Failed to read FLV file: ' . $EVAL_ERROR;
88 }
89
90 #$self->{filehandle} = undef; # implicitly close the filehandle
91 #$self->{pos} = 0;
92
93 return;
94}
95
96
97'done coding';