Created
October 29, 2011 14:41
-
-
Save mizzy/1324519 to your computer and use it in GitHub Desktop.
A script for getting FLV information
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/perl | |
| use strict; | |
| use warnings; | |
| use Readonly; | |
| Readonly my $FLV_TAG_TYPE_AUDIO => 0x08; | |
| Readonly my $AUDIO_MASK_STEREO => 0x01; | |
| Readonly my $AUDIO_MASK_16bit => 0x02; | |
| Readonly my $AUDIO_MASK_RATE => 0x0c; | |
| Readonly my $AUDIO_RATE_5point5KHZ => 0x00; | |
| Readonly my $AUDIO_RATE_11KHZ => 0x04; | |
| Readonly my $AUDIO_RATE_22KHZ => 0x08; | |
| Readonly my $AUDIO_RATE_44KHZ => 0x0c; | |
| Readonly my $AUDIO_MASK_FORMAT => 0xf0; | |
| Readonly my $AUDIO_FORMAT_UNCOMPRESSED => 0x00; | |
| Readonly my $AUDIO_FORMAT_ADPCM => 0x10; | |
| Readonly my $AUDIO_FORMAT_MP3 => 0x20; | |
| Readonly my $AUDIO_FORMAT_NELLYMOSER_8KHZ_MONO => 0x50; | |
| Readonly my $AUDIO_FORMAT_NELLYMOSER => 0x60; | |
| Readonly my %AUDIO_RATE => ( | |
| $AUDIO_RATE_5point5KHZ => 5.5, | |
| $AUDIO_RATE_11KHZ => 11, | |
| $AUDIO_RATE_22KHZ => 22, | |
| $AUDIO_RATE_44KHZ => 44, | |
| ); | |
| Readonly my %AUDIO_FORMAT => ( | |
| $AUDIO_FORMAT_UNCOMPRESSED => 'Uncompressed', | |
| $AUDIO_FORMAT_ADPCM => 'ADPCM', | |
| $AUDIO_FORMAT_MP3 => 'MP3', | |
| $AUDIO_FORMAT_NELLYMOSER_8KHZ_MONO => 'Nellymoser 8kHz mono', | |
| $AUDIO_FORMAT_NELLYMOSER => 'Nellymoser', | |
| ); | |
| my $file = shift; | |
| open my $fh, '<', $file; | |
| my $header = read_header($fh); | |
| while ( ! eof $fh ) { | |
| my $tag = read_tag($fh); | |
| if ( $tag->{type} == $FLV_TAG_TYPE_AUDIO and $tag->{length} ) { | |
| my $first_byte = $tag->{first_byte}; | |
| my $stereo = $first_byte & $AUDIO_MASK_STEREO; | |
| my $audio_size = $first_byte & $AUDIO_MASK_16bit ? 16 : 8; | |
| my $audio_rate = $AUDIO_RATE{ $first_byte & $AUDIO_MASK_RATE } || 'unknown'; | |
| my $audio_format = $AUDIO_FORMAT{ $first_byte & $AUDIO_MASK_FORMAT } || 'unknown'; | |
| print 'audio type: ' . ( $stereo ? 'stereo' : 'mono' ) . "\n"; | |
| print "audio size: $audio_size bit\n"; | |
| print "audio rate: $audio_rate Hz\n"; | |
| print "audio format: $audio_format\n"; | |
| last; | |
| } | |
| } | |
| sub read_header { | |
| my $fh = shift; | |
| read $fh, my $sig, 3; | |
| read $fh, my $version, 1; | |
| read $fh, my $flag, 1; | |
| read $fh, my $offset, 4; | |
| read $fh, my $prev_tag, 4; | |
| return { | |
| sig => $sig, | |
| version => unpack('H*', $version), | |
| flag => unpack('H*', $flag), | |
| offset => unpack('H*', $offset), | |
| }; | |
| } | |
| sub read_tag { | |
| my $fh = shift; | |
| read $fh, my $type, 1; | |
| read $fh, my $length, 3; | |
| read $fh, my $timestamp, 3; | |
| read $fh, my $timestamp_extended, 1; | |
| read $fh, my $stream_id, 3; | |
| read $fh, my $first_byte, 1; | |
| my $pos = hex unpack 'H*', $length; | |
| seek $fh, $pos - 1, 1; | |
| read $fh, my $prev_tag, 4; | |
| return { | |
| type => unpack('C', $type), | |
| length => $pos, | |
| first_byte => unpack('C', $first_byte), | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment