Apache-MP3

 view release on metacpan or  search on metacpan

MP3/Resample.pm  view on Meta::CPAN


sub decompressor {
  my $self = shift;
  unless (%DECOMPRESSORS) {
    my @p;
    if (my $conf = $self->r->dir_config('DecompressorPresets')) {
      @p = split /\s*(?:=>|,)\s*/,$conf;
    } else {
      @p = @DECOMPRESSOR_DEFAULTS;
    }
    my @i = map {$_*2} (0..(@p-1)/2);
    %DECOMPRESSORS    = @p;
    @DECOMPRESSORS = @p[@i];
  }
  my $mime = shift or return @DECOMPRESSORS;
  return $DECOMPRESSORS{$mime};
}

sub sample_popup {
  my $self = shift;
  my @bitrates = $self->presets;
  unshift @bitrates,0;
  my %labels = (0 => '--');
  return (
	  start_form(-name=>'form'),
	  table(
		TR(th('Resample:'),
		   td(
		      popup_menu(-name  => 'bitrate',
				 -value => \@bitrates,
				 -labels => \%labels,
				 -onChange => 'form.submit()'
				),
		     )
		  )
		),
	    end_form);
}

sub directory_top {
  my $self = shift;
  print start_table({-width=>'100%'});
  print start_TR,start_td;
  $self->SUPER::directory_top(@_);
  print end_td;
  print td({-align=>'RIGHT',-valign=>'TOP'},
	   $self->sample_popup());
  print end_TR,end_table;
}

sub open_file {
  my $self = shift;
  return $self->SUPER::open_file(@_) unless $self->bitrate;
  my $file = shift;
  my $bitrate = $self->bitrate;
  my $presets = $self->presets($bitrate);
  my $type = $self->r->lookup_file($file)->content_type;
  my $decompress = $self->decompressor ($type) || "";
  my $encode = $self->r->dir_config('MP3Encoder') || ENCODE;
  my $inputtype = length $decompress ? "" : " --mp3input";
  my $percentF = length $decompress ? "" : ("<" . quotemeta ($file));
  $decompress =~ s{%([a-zA-Z])}
    { $1 eq 'f' ? quotemeta($file) :
	"%$1"}exg;
  $encode =~ s{%([a-zA-Z])}
              {$1 eq 'b' ? $presets         :
	       $1 eq 'D' ? $decompress      :
	       $1 eq 'I' ? $inputtype       :
	       $1 eq 'F' ? $percentF        :
               $1 eq 'f' ? quotemeta($file) :
               "%$1"}exg;
  my $filter = $self->r->dir_config('VerboseMP3Encoder')
    ? "$encode |" : "$encode 2>/dev/null |";
  return IO::File->new($filter);
}

sub read_shorten {
  my $self = shift;
  my ($file,$data) = @_;
  my $sec;
  my $bits = 0;
  my $samples = 0;
  my $channels = 0;
  my $track = "";

  open (SHNINFO, "shntool info \Q$file\E 2>/dev/null|") or return;

  while (<SHNINFO>) {
     if (/^length:\s+(\d+):(\d+)\.(\d+)/) {
	$sec = $1 * 60 + $2;
     } elsif (/^channels:\s+(\d+)/) {
	$channels = $1;
     } elsif (/^samples\/sec:\s+(\d+)/) {
	$samples = $1;
     } elsif (/^bits\/sample:\s+(\d+)/) {
	$bits = $1;
     }
  }
  close SHNINFO;

  %$data = (
	    title  => basename ($file),
	    artist => "",
	    album  => "",
	    year   => "",
	    genre  => "",
	    track  => $track,
	    comment => "",
	    min         => int $sec/60,
	    sec         => $sec % 60,
	    seconds     => $sec,
	    bitrate     => int ($channels * $bits * $samples / 1024),
	    samplerate  => $samples,
	    duration    => sprintf("%dm %2.2ds", int $sec/60,$sec%60),
	   )
}

sub read_flac {
  my $self = shift;
  my ($file,$data) = @_;
  my $sec;
  my $bits = 0;
  my $samples = 0;
  my $channels = 0;
  my $track = "";
  my $rate = 0;

  open (METAFLAC, "metaflac --list --block-number 0 \Q$file\E 2>/dev/null|")
    or return;



( run in 1.603 second using v1.01-cache-2.11-cpan-39bf76dae61 )