#!/usr/bin/perl -w # $Id: vidrename 525 2012-04-15 19:23:51Z svnuser $ # # By Marc MERLIN use strict; use Getopt::Long; my $i; my $j; my $dir; my $file; my $add=""; my $nodots=0; my $norename=0; my $fixepisodes=0; my $extonly=0; my $spacedash=0; my $noinfo=0; my $ucname; my $swap=0; my $writetofile=0; my %codecmap= ( "MPEG1" => ".mpeg", "MPEG2" => ".mpeg", "RV10" => ".rm", "RV13" => ".rm", "RV20" => ".rm", "RV30" => ".rm", "RV40" => ".rm", "MOV" => ".mov", "[mp4v]" => ".mov", "[cvid]" => ".avi", "[CRAM]" => ".avi", "[FMP4]" => ".avi", "[IV32]" => ".avi", "[IV50]" => ".avi", "[I263]" => ".avi", "[I420]" => ".avi", "[MJPG]" => ".avi", "[YVU9]" => ".avi", "[MPG4]" => ".avi", # http://en.wikipedia.org/wiki/Wmv#Versions "[MP42]" => ".wmv", "[MP43]" => ".wmv", "[MP4S]" => ".wmv", "[WMV1]" => ".wmv", "[WMV2]" => ".wmv", "[WMV3]" => ".wmv", "[WVC1]" => ".wmv", "[VP62]" => ".wmv", "[IV41]" => ".wmv", "[SVQ3]" => ".wmv", "[KMVC]" => ".wmv", "[VP61]" => ".wmv", # http://wiki.multimedia.cx/index.php?title=H.264 "[avc1]" => " - H264.avi", "[H264]" => " - H264.avi", "[h264]" => " - H264.avi", "[div3]" => " - divx3.avi", "[DIV3]" => " - divx3.avi", "[DIVX]" => " - divx4.avi", "[divx]" => " - divx4.avi", "[DX50]" => " - divx5.avi", "[dx50]" => " - divx5.avi", "[XVID]" => " - xvid.avi", "[xvid]" => " - xvid.avi", ); sub usage { die < \$fixepisodes, "extonly" => \$extonly, "norename" => \$norename, "nodots" => \$nodots, "spacedash" => \$spacedash, "ucname:i" => \$ucname, "add=s" => \$add, "swap=i" => \$swap, "noinfo" => \$noinfo, "writetofile" => \$writetofile) or usage; $add=" - $add" if ($add); if (defined $ucname) { $ucname=3 if ($ucname == 0); } else { $ucname=0; } open (INVERT, ">/tmp/revertrename") or die "Can't open /tmp/revertrename: $!"; while ($i = shift @ARGV) { my $knownvid="mpeg|avi|wmv|rm|mov|mkv|ogv|mp4"; my $videoline; my @videoline; my $codec; my $sound=""; my $aids=""; my @aids; my $res=""; my $kbps=""; my $codecext; my $shortcodecext; my $ext; my $ogv=0; my $mkv=0; my $mov=0; if ( ! -f $i ) { print "Skipping non file $i\n"; next; } if ( -z $i ) { print "Skipping empty file $i\n"; next; } $i =~ s/"/\\"/g; $i =~ m#^(.*?)/?([^/]+)$#; ($dir,$file) = ($1, $2); $dir.="/" if $dir; # capture all the mplayer file info we an use later $videoline=`alarm 5 mplayer -v -frames 0 -vo null -ao null "$i" 2>/dev/null | egrep "(TiVo file|VIDEO:|Display size:|^AC3: |^DTS: |Found audio stream:|QuickTime/MOV file format detected|^\\\[(mkv|mov)\\\]|Fourcc: mp4v Codec: '3ivx|\\\[Ogg\\\] stream .: video)"`; @videoline = split(/\n/, $videoline); # This is good for knowing if files have multiple audio streams @aids = grep (/Found audio stream:/, @videoline); if ($#aids > 0) { foreach $_ (@aids) { s/.*Found audio stream: // }; $aids = " - aid".join("_", @aids); } $sound = " - $1$2" if ($videoline =~ /(AC3|DTS):( .+?) \(/); $sound .= $aids; # Gross hacks for apple quicktime MOV files, since mplayer gives # different output for them :( $videoline =~ s/Display size: (\w+) x (\w+)/Display size: $1x$2 /; $mov = 1 if ($videoline =~ /\[mov\]/); # Deal with other formats $ogv = 1 if ($videoline =~ /\[Ogg\] stream/); $mkv = 1 if ($videoline =~ /\[mkv\]/); $res=" - $1" if ($videoline =~ / (\d+x\d+) /); $kbps=" - ".$1."kbps" if ($videoline =~ / (\d+).\d+ kbps /); # Reuse the bitrate encoded in the filename if there is one $kbps=" - ".$1."kbps" if (not $kbps and $file =~/(\d+)kbps/); # some codecs give a 0 bitrate in mplayer, get rid of that $kbps =~ s# - 0kbps##; $file =~ s/%20/ /g; $file =~ s/\./ /g if $nodots; $file =~ s/(vob|m2v|mpeg|mpe|mpg)$/mpeg/i; $file =~ s/AVI$/avi/; $file =~ s/(?:xvid|divx|dvix)\d?$/avi/i; $file =~ s/(wmv|wma|asf|asx)$/wmv/i; $file =~ s/(ram|rm)$/rm/i; $file =~ s/(ogg|ogm)$/ogv/i; $file =~ s/(qt|mov)$/mov/i; $file =~ s/(qt|mkv)$/mkv/i; $file =~ s/ ($knownvid)$/.$1/i; $file =~ s/ *.($knownvid)$/.$1/i; $file =~ s/.($knownvid)\s+$/.$1/i; if (not $extonly) { $file =~ s/ *\[[^ -]*\]\././; # we need to remove the possibly added aid here before _'s are removed $file =~ s/ - aid[0-9_]+//; $file =~ s/_/ /g; # Remove crap at the beginning of some video files $file =~ s#^\[([^-\]]+)\]#$1 #; $file =~ s#^\(([^-)]+)\)#$1 #; $file =~ s#(? $codec /$res /$kbps\n"; die "No mapping for codec $codec (file $i)\n" unless defined $codecmap{$codec}; $codecext=$codecmap{$codec}; # Fix codec extensions now # Ogg is not actually avi, fix this now $codecext =~ s/.avi$/.ogv/ if ($ogv); # same with mkv $codecext =~ s/.avi$/.mkv/ if ($mkv); # same with mov $codecext =~ s/.avi$/.mov/ if ($mov); # Mplayer can read Tivo ty files, but they're not real mpeg2s $codecext=" - TivoTy.mpeg" if ($videoline =~ /TiVo file/); # These are special MOVs, let's tag them as such $codecext=" - 3ivx.mov" if ($videoline =~ /Fourcc: mp4v Codec: '3ivx/); ($shortcodecext = $codecext) =~ s/.*\.(\w+)/$1/; if ($file !~ /\.(\w+)$/) { warn "Skipping $i, could not read extension\n"; next; } else { $ext=$1; warn "Warning for $i, $shortcodecext != $ext\n" if ($ext ne $shortcodecext); } # Now we can remove the old extension and regenerate a good one later $file =~ s/\.\w+$//; $file =~ s/ - divx.$//i; $file =~ s/ - xvid$//i; $file =~ s/ - ogm$//i; $file =~ s/ - ogv$//i; $file =~ s/ - ogg$//i; $file =~ s/ - H264$//i; # Remove crap: $file =~ s/Eurosport.x264.*//i; $file =~ s/Motors.?TV.x264.*//i; if (not $extonly) { if ($spacedash) { $file =~ s#([^ ])-#$1 -#g; $file =~ s#-([^ ])#- \u$1#g; } if ($fixepisodes) { $file =~ s#\[(\d+)x(\d\d)\]#$1x$2#i; $file =~ s#S(\d+)E(\d\d)#$1x$2#i; $file =~ s# (\d)(\d\d) # $1x$2 #; $file =~ s# *(\d\d?x\d\d) *- *# - $1 #; $file =~ s#([^-]) *(\d\d?x\d\d) *([^-])#$1 - $2 $3#; $file =~ s#- *(\d\d?x\d\d) *- *#- $1 #; $file =~ s# - (\d\d?x\d\d) (\S)# - $1 $2#; } $file =~ s# - (\d\d) (.)# - $1 \u$2#; $file =~ s#^(.)#\u$1#; $file =~ s#^(.*[^ ]) *- *(.*[^ ])$#\u$1 - \u$2#; $file =~ s/((?:\S+){$ucname,})/\u$1/g if $ucname; $file =~ s/- -/-/g; $file =~ s/\s+$//; $file =~ s/ - (AC3|DTS) \d.\d$//; $file =~ s/ - \d+kbps$//; $file =~ s/ - \d\d\d+x\d\d\d+$//; $file =~ s/(\d\d\d\d).(\d\d).(\d\d)/$1-$2-$3/; if ($noinfo) { $file =~ s/$/$add$codecext/; } else { $file =~ s/$/$add$res$kbps$sound$codecext/; } } else { $file =~ s/$/$add$codecext/; } $file =~ s/- -/-/g; if ($writetofile) { my $str="$ext | $codec/$codecext | $res | $kbps | $sound"; $str =~ s/\[//g; $str =~ s/]//g; $str =~ s/\.avi//g; $str =~ s/ - //g; print "$i: $str\n"; next; } $j="$dir$file"; if ($i ne $j) { print "Going from $i \nto $j\n"; if (not $norename) { die "Not overwriting \"$j\"" if -e "$j"; rename($i,$j) or die "rename \"$i\" \"$j\" failed: $!\n"; } print INVERT "/bin/mv \"$j\" \"$i\"\n"; } else { print "$i will be left unchanged\n"; } }