批量修改文件名 主要用于让字幕文件名与影片文件名保持一致

#!/usr/bin/perl

=pod

=encoding utf8

=head1 NAME

correct_subtitle_filename.pl

=head1 SYNOPSIS

根据视频文件的名字来修改字幕文件的名字。

=head1 REVISION

beta by barrypp at 201212311006
v1.0  by barrypp at 20121231143850

=head1 TODO



=head1 AUTHOR

barrypp 

=cut

use 5.010;
#use strict;
use warnings;
use diagnostics;
use utf8;
use Cwd;

use Encode qw\decode\;
use Win32::Codepage::Simple qw\get_codepage\;


my $in_filename_extension = "mkv";
my $out_filename_extension = "ass";
my $in_filename_re = '-([0-9]{2})-';
my $out_filename_re = '\[([0-9]{2})\]';
my $in_dir = ".";
my $out_dir = "subs";

my $code_page = "cp" . get_codepage();
my %Names = ();


opendir (my $IN_DIR,$in_dir);
while (defined (my $file = readdir $IN_DIR)){
	if (-f $file and $file =~ m/\.$in_filename_extension\z/ and (decode $code_page,$file) =~ m/$in_filename_re/){
		if(exists $Names{$1}){
			die "error:two files have the same serial number. Correct the regular exp";
		}else{
			my $tmp = $1;
			$file =~ m/\.$in_filename_extension\z/p;
			$Names{$tmp} = ${^PREMATCH};
		}
	}
}
closedir $IN_DIR;

{#for debug only
	my($a,$b);
	say "$a => $b" while(($a,$b) = each %Names);
}

opendir (my $OUT_DIR,$out_dir);chdir $out_dir;
while (defined (my $file = readdir $OUT_DIR)){
	if (-f $file and $file =~ m/\.$out_filename_extension\z/ and (decode $code_page,$file) =~ m/$out_filename_re/){
		if(exists $Names{$1}){
			say "rename $file => ",$Names{$1},".$out_filename_extension";
			rename $file => $Names{$1} . ".$out_filename_extension";
		}else{
			say "warning:substitles is out of control ($file)";
		}
	}
}
closedir $OUT_DIR;

 
  

你可能感兴趣的:(技术)