Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的ID3标记-Python是用于此任务的错误语言吗?_Python_Perl_Id3 - Fatal编程技术网

Python中的ID3标记-Python是用于此任务的错误语言吗?

Python中的ID3标记-Python是用于此任务的错误语言吗?,python,perl,id3,Python,Perl,Id3,我正在尝试编写一个Python脚本来读取我的MP3文件名,当它发现缺少一个艺术家标记时,它将从文件名的第一部分获取它。我的大多数mp3都命名为Artist-Title.mp3 但是没有一个ID3标记读取器在Python中工作得很好。ID3不会读取超过1.1的任何标记,并且自2002年以来就没有开发过。诱变剂在文件缺少标记时引发异常。Eye3D需要安装二进制文件才能使库正常工作,pylibid3也是如此 我用错语言了吗?我听说Perl有一些很棒的ID3标记库。我是个新手,所以切换语言已经有一段时间

我正在尝试编写一个Python脚本来读取我的MP3文件名,当它发现缺少一个艺术家标记时,它将从文件名的第一部分获取它。我的大多数mp3都命名为Artist-Title.mp3

但是没有一个ID3标记读取器在Python中工作得很好。ID3不会读取超过1.1的任何标记,并且自2002年以来就没有开发过。诱变剂在文件缺少标记时引发异常。Eye3D需要安装二进制文件才能使库正常工作,pylibid3也是如此

我用错语言了吗?我听说Perl有一些很棒的ID3标记库。我是个新手,所以切换语言已经有一段时间没有读过关于Perl的书了,这意味着要从头开始。但是如果Python是错误的语言,我愿意这么做


有什么想法吗?

用诱变剂处理异常很容易:

from mutagen.id3 import ID3, TPE1, ID3NoHeaderError
try:
    audio = ID3(filename)
except ID3NoHeaderError:
    audio = ID3()

audio.add(TPE1(encoding=3, text=u'Artist'))
audio.save(filename)

使用诱变剂处理异常非常简单:

from mutagen.id3 import ID3, TPE1, ID3NoHeaderError
try:
    audio = ID3(filename)
except ID3NoHeaderError:
    audio = ID3()

audio.add(TPE1(encoding=3, text=u'Artist'))
audio.save(filename)

下面是一个Perl脚本,它可以满足您的需要。我一直用这个。它甚至在进行更改之前预览更改

您确实需要安装两个Perl模块MP3::Info和MP4::Info,尽管您可以从代码中删除MP4行并跳过该模块

#!/usr/bin/perl

use strict;
use warnings;
use Cwd;
use File::Copy;
use File::Basename;

# Load MP3/MP4 modules and set them up to use UTF-8 characters
# (Unicode-like support, to see accents, etc.).
use MP3::Info qw(:all);
use_mp3_utf8(1);
use MP4::Info qw(:all);
use_mp4_utf8(1);

my @ARGS;
################################################################################
# Subroutine: RemoveIllegalFilenameCharacters
# Inputs:     $filename
# Outputs:    $filename
################################################################################
sub RemoveIllegalFilenameCharacters {
    my $filename = shift;

    if ($filename =~ m/\\/) { $filename =~ s/\\//g }
    if ($filename =~ m/\//) { $filename =~ s/ \/ / - /g }
    if ($filename =~ m/\//) { $filename =~ s/\///g }
    if ($filename =~ m/:/) { $filename =~ s/://g }
    if ($filename =~ m/\*/) { $filename =~ s/\*//g }
    if ($filename =~ m/\?/) { $filename =~ s/\?//g }
    if ($filename =~ m/"/) { $filename =~ s/"//g }
    if ($filename =~ m/</) { $filename =~ s/<//g }
    if ($filename =~ m/>/) { $filename =~ s/>//g }
    if ($filename =~ m/\|/) { $filename =~ s/\|//g }

    return $filename;
}

################################################################################
# Subroutine: Rename
# Inputs:     $test (indicates test mode)
# Outputs:    number of files to be changed (in test mode)
################################################################################
sub Rename {
  my $test = shift;
  my $destDir = ""; # hard-coded permanent destination, if desired

  my @tests;
  foreach my $file (@ARGS) {
    # Get rid of the Mac OS resource fork files.
    if ($file =~ m/^\._/) {
      unlink "$file";
      next;
    }

    if (! -f $file) {
      warn "'$file' does not exist!\n";
    }
    else {
      # If $destDir wasn't set above, then that means it should be
      # set to the original dir of each file.
      if (!($destDir)) {
    $destDir = dirname($file);
      }

      my $extension = $file;
      $extension =~ s/.*\.//;

      my $tag;
      if ($extension =~ m/^mp3$/i) {
    ($tag = get_mp3tag($file)) || warn "'$file' does not contain ID3 tags (it may not even be an MP3 file!)\n";
      }
      else {
    # If it's not MP3, try MP4
    ($tag = get_mp4tag($file)) || warn "'$file' does not contain ID3 tags (it may not even be an AAC/M4A/M4P file!)\n";
      }
      if (!($tag)) {
    # No $tag was returned.  Go to the next $file.
    next;
      }

      # DEBUG!  Show all the tag names.  Could be modifed to show all the tag values too.
      if (0) {
    print join("\n", keys %{$tag}) . "\n";
      }

      # Set the rename format depending on if we're under the $DOWNLOAD_DIR or not.
      my $newFile = $$tag{"ARTIST"} . " - " . $$tag{"TITLE"} . ".$extension";

      if (($$tag{"ARTIST"} eq "") || ($$tag{"TITLE"} eq "")) { warn "\n*** WARNING! This track is missing some info:\n\tOriginal Name: $file\n\tNew Name: $newFile\n\n" }
      $newFile = RemoveIllegalFilenameCharacters($newFile);

      # If current filename ($file) already matches the new filename ($newFile),
      # don't bother continuing (filename is already in the correct format).
      if ($file eq $newFile) {
    # If we're not choosing to move all files,
    # and if the filename is already in the correct format,
    # go to the next $file.
    next;
      }
      if ($destDir ne ".") { $newFile = $destDir . "/" . $newFile }
      if (!($test) && -f $newFile) {
    die "Unable to move '$file' to '$newFile',\nsince there's already a file named '$newFile'!\nStopped";
      }
      if ($test) {
    push @tests, $newFile;
      } else {
    print "Moving '$file' to '$newFile'\n";
    move($file, $newFile) || die "Unable to move file!\n";
      }
    } # End of if-then-else checking if file exists
  } # End of FOR loop looping through files in @ARGS.

  if ($test && scalar(@tests)) {
    print "Test run - new filenames: \n  ";
    print join("\n  ", sort(@tests));
  }

  return scalar(@tests);
}

################################################################################
# MAIN ROUTINE

if (scalar(@ARGV) == 0) {
    # If no args, use every music file in current directory.
    opendir(DIR, ".");
    @ARGS = sort(grep(/\.mp3$|\.aac$|\.m4a$|\.m4p$/i, readdir(DIR)));
    closedir(DIR);

    if (scalar(@ARGS) == 0) {
      print "USAGE: " . basename($0) . " <music-files>\n\n";
      print "If no filenames are specified, any music files (MP3/AAC/M4A/M4P) in the current directory will be used.\n";
      exit 1;
    }
}
else { @ARGS = @ARGV }

if (Rename(1)) {
    print "\n\nDo the test results look good?\n";
    print " n  - No they don't.  Do not rename the files!\n";
    print "[y] - Yes they do.  Rename the files and leave them in their original folder(s).\n";
    print " ";
    my $choice = <STDIN>;
    chomp $choice;
    # Only do the move if we're in the $DOWNLOAD_DIR area.
    # This allows us to use the default (null) response to process renames in a non-download dir.
    if (("$choice" eq "") || ("$choice" eq "y")) {
    Rename(0);
    }
}
else {
    print "No actions needed.\n";
}

下面是一个Perl脚本,它可以满足您的需要。我一直用这个。它甚至在进行更改之前预览更改

您确实需要安装两个Perl模块MP3::Info和MP4::Info,尽管您可以从代码中删除MP4行并跳过该模块

#!/usr/bin/perl

use strict;
use warnings;
use Cwd;
use File::Copy;
use File::Basename;

# Load MP3/MP4 modules and set them up to use UTF-8 characters
# (Unicode-like support, to see accents, etc.).
use MP3::Info qw(:all);
use_mp3_utf8(1);
use MP4::Info qw(:all);
use_mp4_utf8(1);

my @ARGS;
################################################################################
# Subroutine: RemoveIllegalFilenameCharacters
# Inputs:     $filename
# Outputs:    $filename
################################################################################
sub RemoveIllegalFilenameCharacters {
    my $filename = shift;

    if ($filename =~ m/\\/) { $filename =~ s/\\//g }
    if ($filename =~ m/\//) { $filename =~ s/ \/ / - /g }
    if ($filename =~ m/\//) { $filename =~ s/\///g }
    if ($filename =~ m/:/) { $filename =~ s/://g }
    if ($filename =~ m/\*/) { $filename =~ s/\*//g }
    if ($filename =~ m/\?/) { $filename =~ s/\?//g }
    if ($filename =~ m/"/) { $filename =~ s/"//g }
    if ($filename =~ m/</) { $filename =~ s/<//g }
    if ($filename =~ m/>/) { $filename =~ s/>//g }
    if ($filename =~ m/\|/) { $filename =~ s/\|//g }

    return $filename;
}

################################################################################
# Subroutine: Rename
# Inputs:     $test (indicates test mode)
# Outputs:    number of files to be changed (in test mode)
################################################################################
sub Rename {
  my $test = shift;
  my $destDir = ""; # hard-coded permanent destination, if desired

  my @tests;
  foreach my $file (@ARGS) {
    # Get rid of the Mac OS resource fork files.
    if ($file =~ m/^\._/) {
      unlink "$file";
      next;
    }

    if (! -f $file) {
      warn "'$file' does not exist!\n";
    }
    else {
      # If $destDir wasn't set above, then that means it should be
      # set to the original dir of each file.
      if (!($destDir)) {
    $destDir = dirname($file);
      }

      my $extension = $file;
      $extension =~ s/.*\.//;

      my $tag;
      if ($extension =~ m/^mp3$/i) {
    ($tag = get_mp3tag($file)) || warn "'$file' does not contain ID3 tags (it may not even be an MP3 file!)\n";
      }
      else {
    # If it's not MP3, try MP4
    ($tag = get_mp4tag($file)) || warn "'$file' does not contain ID3 tags (it may not even be an AAC/M4A/M4P file!)\n";
      }
      if (!($tag)) {
    # No $tag was returned.  Go to the next $file.
    next;
      }

      # DEBUG!  Show all the tag names.  Could be modifed to show all the tag values too.
      if (0) {
    print join("\n", keys %{$tag}) . "\n";
      }

      # Set the rename format depending on if we're under the $DOWNLOAD_DIR or not.
      my $newFile = $$tag{"ARTIST"} . " - " . $$tag{"TITLE"} . ".$extension";

      if (($$tag{"ARTIST"} eq "") || ($$tag{"TITLE"} eq "")) { warn "\n*** WARNING! This track is missing some info:\n\tOriginal Name: $file\n\tNew Name: $newFile\n\n" }
      $newFile = RemoveIllegalFilenameCharacters($newFile);

      # If current filename ($file) already matches the new filename ($newFile),
      # don't bother continuing (filename is already in the correct format).
      if ($file eq $newFile) {
    # If we're not choosing to move all files,
    # and if the filename is already in the correct format,
    # go to the next $file.
    next;
      }
      if ($destDir ne ".") { $newFile = $destDir . "/" . $newFile }
      if (!($test) && -f $newFile) {
    die "Unable to move '$file' to '$newFile',\nsince there's already a file named '$newFile'!\nStopped";
      }
      if ($test) {
    push @tests, $newFile;
      } else {
    print "Moving '$file' to '$newFile'\n";
    move($file, $newFile) || die "Unable to move file!\n";
      }
    } # End of if-then-else checking if file exists
  } # End of FOR loop looping through files in @ARGS.

  if ($test && scalar(@tests)) {
    print "Test run - new filenames: \n  ";
    print join("\n  ", sort(@tests));
  }

  return scalar(@tests);
}

################################################################################
# MAIN ROUTINE

if (scalar(@ARGV) == 0) {
    # If no args, use every music file in current directory.
    opendir(DIR, ".");
    @ARGS = sort(grep(/\.mp3$|\.aac$|\.m4a$|\.m4p$/i, readdir(DIR)));
    closedir(DIR);

    if (scalar(@ARGS) == 0) {
      print "USAGE: " . basename($0) . " <music-files>\n\n";
      print "If no filenames are specified, any music files (MP3/AAC/M4A/M4P) in the current directory will be used.\n";
      exit 1;
    }
}
else { @ARGS = @ARGV }

if (Rename(1)) {
    print "\n\nDo the test results look good?\n";
    print " n  - No they don't.  Do not rename the files!\n";
    print "[y] - Yes they do.  Rename the files and leave them in their original folder(s).\n";
    print " ";
    my $choice = <STDIN>;
    chomp $choice;
    # Only do the move if we're in the $DOWNLOAD_DIR area.
    # This allows us to use the default (null) response to process renames in a non-download dir.
    if (("$choice" eq "") || ("$choice" eq "y")) {
    Rename(0);
    }
}
else {
    print "No actions needed.\n";
}

一个快速的谷歌搜索结果是:。你考虑过所有这些模块吗?请继续使用诱变剂,它是目前最好的ID3库。我发现它工作得非常好,比Perl语言中的任何东西都好,甚至比taglib绑定都好。Python中的异常并不罕见:捕捉它们并根据需要继续。此外,投票以非建设性方式结束:问题征求意见,辩论-请参见SO网站常见问题解答。谷歌快速得出以下结论:。你考虑过所有这些模块吗?请继续使用诱变剂,它是目前最好的ID3库。我发现它工作得非常好,比Perl语言中的任何东西都好,甚至比taglib绑定都好。Python中的异常并不罕见:捕捉它们并根据需要继续。此外,投票以不具建设性的方式结束:问题征求意见,辩论-请参见SO网站FAQ.True。我试试看,谢谢。我想当我使用诱变剂时,我感觉自己遇到了麻烦。似乎不起作用:我得到了这个:诱变剂。id3。ID3NoHeaderError:'E:\.mp3'不是以id3开头的tag@Tensigh,我很惊讶,它对我很有效。我试试看,谢谢。我想当我使用诱变剂时,我感觉自己遇到了麻烦。似乎不起作用:我得到了这个:诱变剂。id3。ID3NoHeaderError:'E:\.mp3'不是以id3开头的tag@Tensigh,我很惊讶,它对我很有效。谢谢。我已经安装了Perl,但我已经将全部精力投入到Python中。我开始这样做的原因之一是为了学习编程。我在Perl中看到了很多优秀的东西,这无疑也促使我学习Perl。谢谢你的剧本,谢谢。我已经安装了Perl,但我已经将全部精力投入到Python中。我开始这样做的原因之一是为了学习编程。我在Perl中看到了很多优秀的东西,这无疑也促使我学习Perl。谢谢你的剧本。