LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 925|回复: 3

将rmvb文件的metadata由GBK转换成UTF8

[复制链接]
发表于 2007-9-18 04:21:37 | 显示全部楼层 |阅读模式
那位知道有什么软件可以将rmvb的tag转换成UTF8的?
 楼主| 发表于 2007-9-21 10:47:32 | 显示全部楼层
算了,算了,自己写了一个perl script

11/06/2007 稍微改了一下
  1. #!/usr/bin/perl
  2. #this is to convert RMVB file's metadata from GBK to UTF8 encoding
  3. #to use it:
  4. #        convRmvbEncode fileName.rmvb
  5. #
  6. #************USE AT YOUR OWN RISK******************
  7. #If the RMVB file has more than one DATA section, the result file
  8. #may not be usable.
  9. #The RMVB file format can be found at:
  10. #http://wiki.multimedia.cx/index.php?title=RealMedia
  11. use strict 'vars';
  12. use Fcntl; #for random file control
  13. use Encode qw/from_to/;
  14. #*****************************************************
  15. my $from_encoding = "GBK";        #old file's encoding
  16. my $to_encoding = "UTF8";        #new file's encoding
  17. #*****************************************************
  18. my $rmvbFile = @ARGV[0];
  19. my ($buffer); #temp variable
  20. #-----------------------------------------------------
  21. #the following to try to read CONT tag
  22. #and covert the CONT tags to UTF8
  23. my $changed_cont = ""; #cont section after coverting to UTF8
  24. my $added_len = -1; #number of chars added to cont
  25.                     #section after coverting to UTF8
  26. my $num_headers = 0;
  27. {
  28.         open(HND_RMVB, "<$rmvbFile")
  29.                 or die "Couldn't open $rmvbFile for overwriting: $!\n";
  30.         binmode(HND_RMVB);
  31.         #---------------------------------------------
  32.         #try to read content tag of the rmvb file
  33.         my ($cont_pos, $cont_size, $cont_ver, $cont_buffer) =
  34.                 (-1, -1, -1, "");
  35.        
  36.         #read first 18 bytes
  37.         $buffer = "";
  38.         read(HND_RMVB, $buffer, 18) == 18 or die "error when read:$!";
  39.         my ($obj_id, $obj_size, $obj_version, $file_ver);
  40.         ($obj_id,
  41.             $obj_size,
  42.             $obj_version,
  43.             $file_ver,
  44.             $num_headers) = unpack("a4NnNN", $buffer);
  45.         #quit if not a real media file
  46.         die "Not a Real Media file" if $obj_id ne ".RMF";
  47.         for (my $i = 0; $i < $num_headers; $i ++){
  48.                 $buffer = "";
  49.                 read(HND_RMVB, $buffer, 10) == 10
  50.                         or die "error when read:$!";
  51.                 ($obj_id, $obj_size, $obj_version)
  52.                         = unpack("a4NH", $buffer);
  53.                 print "Header $i: $obj_id, $obj_size, $obj_version\n";
  54.                 if ($obj_id eq "CONT") {
  55.                         $cont_buffer = "";
  56.                         read(HND_RMVB, $cont_buffer, $obj_size - 10 )
  57.                                 == $obj_size - 10
  58.                                 or die "error when read:$!";
  59.                         $cont_pos = tell(HND_RMVB) - $obj_size;
  60.                         $cont_size = $obj_size;
  61.                         $cont_ver = $obj_version;
  62.                 }else{
  63.                         #skip other sections
  64.                         seek(HND_RMVB,
  65.                              tell(HND_RMVB) + $obj_size - 10,
  66.                              SEEK_SET);
  67.                 }
  68.         }
  69.         die "Couldn't find content tags" if ($cont_size == -1);
  70.         #-------------------------------------------------------
  71.        
  72.         #the following will convert content tag to UTF encoding
  73.         #------------------------------------------------------
  74.         my ($title_len, $title);
  75.         my ($author_len, $author);
  76.         my ($copyright_len, $copyright);
  77.         my ($comment_len, $comment);
  78.        
  79.         my $tmp_pos = 0;
  80.         #read title
  81.         $title_len = unpack("n", substr($cont_buffer,$tmp_pos, 2));
  82.         $tmp_pos += 2;
  83.         $title = substr($cont_buffer, $tmp_pos, $title_len);
  84.         $tmp_pos += $title_len;
  85.         #read author
  86.         $author_len = unpack("n", substr($cont_buffer,$tmp_pos, 2));
  87.         $tmp_pos += 2;
  88.         $author = substr($cont_buffer, $tmp_pos, $author_len);
  89.         $tmp_pos += $author_len;
  90.         #read copyright
  91.         $copyright_len = unpack("n", substr($cont_buffer,$tmp_pos, 2));
  92.         $tmp_pos += 2;
  93.         $copyright = substr($cont_buffer, $tmp_pos, $copyright_len);
  94.         $tmp_pos += $copyright_len;
  95.         #read comment
  96.         $comment_len = unpack("n", substr($cont_buffer,$tmp_pos, 2));
  97.         $tmp_pos += 2;
  98.         $comment = substr($cont_buffer, $tmp_pos, $comment_len);
  99.         $tmp_pos += $comment_len;
  100.         print "------Before Change-------\n";
  101.         print "title($title_len)=$title\n";
  102.         print "author($author_len)=$author\n";
  103.         print "copyright($copyright_len)=$copyright\n";
  104.         print "comment($comment_len)=$comment\n";
  105.         #convert to UTF8
  106.         from_to($title, $from_encoding, $to_encoding);
  107.         from_to($author, $from_encoding, $to_encoding);
  108.         from_to($copyright, $from_encoding, $to_encoding);
  109.         from_to($comment, $from_encoding, $to_encoding);
  110.         print "------After Change-------\n";
  111.         $title_len = length($title);
  112.         $author_len = length($author);
  113.         $copyright_len = length($copyright);
  114.         $comment_len = length($comment);
  115.         print "title($title_len)=$title\n";
  116.         print "author($author_len)=$author\n";
  117.         print "copyright($copyright_len)=$copyright\n";
  118.         print "comment($comment_len)=$comment\n";
  119.                
  120.         #rebuid CONT section
  121.         $changed_cont = pack
  122.                         "a4 N n na$title_len na$author_len na$copyright_len na$comment_len",
  123.                         "CONT",
  124.                         10 +
  125.                         2 + $title_len +
  126.                         2 + $author_len +
  127.                         2 + $copyright_len +
  128.                         2 + $comment_len,
  129.                         $cont_ver,
  130.                         $title_len, $title,
  131.                         $author_len, $author,
  132.                         $copyright_len, $copyright,
  133.                         $comment_len, $comment
  134.                         ;
  135.                        
  136.         $added_len = length($changed_cont) - $cont_size;       
  137.        
  138.         close (HND_RMVB);
  139. }
  140. #----------------------------------------------------------
  141. #confirm with user if want to make the change
  142. print "This Real Media file may have multiple DATA sections, output file may not be usable\n" if ($num_headers > 7);
  143. print "Make the change?(Y/N)\n";
  144. my $user_in=<STDIN>;
  145. $user_in = uc($user_in);
  146. my $newFileCreated = 0;
  147. if (substr($user_in,0,1) ne "Y"){
  148.         print "File not changed\n";
  149. }else{
  150.         open(HND_RMVB, "<$rmvbFile")
  151.                 or die "Couldn't open $rmvbFile for overwriting: $!\n";
  152.         binmode(HND_RMVB);
  153.         #create a new file
  154.         open(NEW_RMVB, ">$rmvbFile.new")
  155.                 or die "error when creating new file:$!";
  156.         binmode(NEW_RMVB);
  157.         #copy the old file's first 18 bytes
  158.         $buffer = "";
  159.         read(HND_RMVB, $buffer, 18) == 18
  160.                 or die "Couldn't read: $!";
  161.                
  162.         print NEW_RMVB $buffer;
  163.        
  164.         my ($obj_id,
  165.             $obj_size,
  166.             $obj_version,
  167.             $file_ver,
  168.             $num_headers) = unpack("a4NnNN", $buffer);
  169.         #print "$obj_id, $obj_size, $obj_version, $file_ver, $num_headers\n";
  170.         for (my $i = 0; $i < $num_headers; $i ++){
  171.                 $buffer = "";
  172.                 read(HND_RMVB, $buffer, 10) == 10
  173.                         or die "error when read:$!";
  174.                 ($obj_id, $obj_size, $obj_version)
  175.                         = unpack("a4NH", $buffer);
  176.                 #print "Header $i: $obj_id, $obj_size, $obj_version\n";
  177.                 if ($obj_id eq "CONT") {
  178.                         print NEW_RMVB $changed_cont;
  179.                        
  180.                         #skip old file's CONT section
  181.                         seek(HND_RMVB,
  182.                              tell(HND_RMVB) + $obj_size - 10,
  183.                              SEEK_SET);
  184.                
  185.                 }elsif ($obj_id eq "PROP"){
  186.                         #rebuid PROP section
  187.                         #need to recalculate offset of DATA section
  188.                         print NEW_RMVB $buffer;
  189.                        
  190.                         $buffer = "";
  191.                         read(HND_RMVB, $buffer, $obj_size - 10);
  192.                         my ($maxBit, $avgBit, $lagData, $avgData,
  193.                          $nOfPcks, $duration, $sugNofMs, $offsetIndx,
  194.                          $offsetData, $nOfsteam, $flag) =
  195.                                         unpack("NNNNNNNNNnn", $buffer);
  196.                                        
  197.                         $buffer = "";
  198.                         $buffer = pack("NNNNNNNNNnn",
  199.                                         $maxBit,
  200.                                         $avgBit,
  201.                                         $lagData,
  202.                                         $avgData,
  203.                                         $nOfPcks,
  204.                                         $duration,
  205.                                         $sugNofMs,
  206.                                         $offsetIndx + $added_len,
  207.                                         $offsetData + $added_len,
  208.                                         $nOfsteam,
  209.                                         $flag
  210.                                         );
  211.        
  212.                         print NEW_RMVB $buffer;
  213.                                                      
  214.                 }else{
  215.                
  216.                         #for other sections, just copy
  217.                        
  218.                         #copy the header already read
  219.                         print NEW_RMVB $buffer;
  220.                        
  221.                         #copy the rest of this section
  222.                         $buffer = "";
  223.                         read(HND_RMVB, $buffer, $obj_size - 10);
  224.                         print NEW_RMVB $buffer;
  225.                        
  226.                 }
  227.         }
  228.        
  229.         close (HND_RMVB);
  230.         close (NEW_RMVB);
  231.        
  232.         rename($rmvbFile, "$rmvbFile.old") or die "Can't rename: $!";
  233.         rename("$rmvbFile.new", "$rmvbFile") or die "Can't rename: $!";
  234. }
复制代码
回复 支持 反对

使用道具 举报

发表于 2007-9-21 15:46:31 | 显示全部楼层
好东西,收藏!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-9-22 00:59:43 | 显示全部楼层
似乎还有些不对的地方,待俺改改再说。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表