FLV::ToMP3 - Convert audio from a FLV file into an MP3 file


FLV-Info documentation Contained in the FLV-Info distribution.

Index


Code Index:

NAME

Top

FLV::ToMP3 - Convert audio from a FLV file into an MP3 file

LICENSE

Top

See FLV::Info

SYNOPSIS

Top

   use FLV::ToMP3;
   my $converter = FLV::ToMP3->new();
   $converter->parse_flv($flv_filename);
   $converter->save($mp3_filename);

See also flv2mp3.

DESCRIPTION

Top

Extracts audio data from an FLV file and constructs an MP3 file. See the flv2mp3 command-line program for a nice interface and a detailed list of caveats and limitations.

METHODS

Top

$pkg->new()

Instantiate a converter.

$self->parse_flv($flv_filename)

Open and parse the specified FLV file.

$self->save($mp3_filename)

Write out an MP3 file. Note: this is usually called only after parse_flv(). Throws an exception upon error.

AUTHOR

Top

See FLV::Info


FLV-Info documentation Contained in the FLV-Info distribution.
package FLV::ToMP3;

use warnings;
use strict;
use 5.008;

use FLV::File;
use FLV::Util;
use FLV::AudioTag;
use English qw(-no_match_vars);
use Carp;

our $VERSION = '0.24';

sub new
{
   my $pkg = shift;

   my $self = bless { flv => FLV::File->new() }, $pkg;
   $self->{flv}->empty();
   return $self;
}

sub parse_flv
{
   my $self   = shift;
   my $infile = shift;

   $self->{flv}->parse($infile);
   $self->{flv}->populate_meta();

   $self->_validate();

   return;
}

sub _validate
{
   my $self = shift;

   my $acodec = $self->{flv}->get_meta('audiocodecid');
   if (!defined $acodec)
   {
      die "No audio data found\n";
   }
   if ($acodec != 2)
   {
      die "Audio format $AUDIO_FORMATS{$acodec} not supported; "
          . "only MP3 audio allowed\n";
   }
   return;
}

sub save
{
   my $self    = shift;
   my $outfile = shift;

   $self->_validate();

   my $outfh = FLV::Util->get_write_filehandle($outfile);
   if (!$outfh)
   {
      die 'Failed to write MP3 file: ' . $OS_ERROR;
   }

   for my $tag ($self->{flv}->{body}->get_tags())
   {
      next if (!$tag->isa('FLV::AudioTag'));
      print {$outfh} $tag->{data};
   }
   close $outfh or die 'Failed to finish writing file';
   return;
}

1;

__END__