Advertisement
Rusnura

C# code for MP3 tag renaming

Jul 17th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace MusicIDv3Renamer
  13. {
  14.     public partial class mainForm : Form
  15.     {
  16.         public mainForm()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.  
  21.         private void openFiles()
  22.         {
  23.             if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  24.             {
  25.                 musicsPathListBox.Items.AddRange(openFileDialog1.FileNames);
  26.             }
  27.         }
  28.  
  29.         private void addBtn_Click(object sender, EventArgs e)
  30.         {
  31.             openFiles();
  32.         }
  33.  
  34.         private String getNameFromIDv3Tags(String path)
  35.         {
  36.             try
  37.             {
  38.                 TagLib.File mp3 = TagLib.File.Create(path);
  39.                 String artist = String.Join(", ", mp3.Tag.Performers);
  40.                 artist = toUtf8(artist);
  41.  
  42.                 String name = mp3.Tag.Title;
  43.                 name = toUtf8(name);
  44.  
  45.                 return (artist + " - " + name).Trim();
  46.             }
  47.             catch (Exception e)
  48.             {
  49.                 return "ERROR [" + e.Message + "]";
  50.             }
  51.         }
  52.  
  53.         private void rename(String path, String newFileName)
  54.         {
  55.             try
  56.             {
  57.                 String directory = Path.GetDirectoryName(path);
  58.                 String ext = (path.Split('.')[(path.Split('.').Length - 1)]);
  59.                 String newPath = directory + "\\" + newFileName + "." + ext;
  60.                 System.IO.File.Move(path, newPath);
  61.             }
  62.             catch (Exception e)
  63.             {
  64.                 try
  65.                 {
  66.                     logListBox.Items.Add("Не удалось переименовать файл " + Path.GetFileName(path) + " в " + Path.GetFileName(newFileName) + ". [" + e.Message + "]");
  67.                 } catch { }
  68.             }
  69.         }
  70.  
  71.         private void goBtn_Click(object sender, EventArgs e)
  72.         {
  73.             if (musicsPathListBox.Items.Count <= 0)
  74.             {
  75.                 MessageBox.Show("Файлы не добавлены :(", "Пустой список", MessageBoxButtons.OK, MessageBoxIcon.Error);
  76.                 return;
  77.             }
  78.  
  79.             goBtn.Enabled = false;
  80.             addBtn.Enabled = false;
  81.             musicsPathListBox.Enabled = false;
  82.             foreach (String mp3 in musicsPathListBox.Items)
  83.             {
  84.                 String newSongName = getNameFromIDv3Tags(mp3);
  85.                 if (newSongName.StartsWith("ERROR ["))
  86.                 {
  87.                     try
  88.                     {
  89.                         logListBox.Items.Add("Не удалось получить теги с файла " + Path.GetFileName(mp3) + ": " + newSongName);
  90.                     }
  91.                     catch { }
  92.                 }
  93.                 else
  94.                 {
  95.                     if (newSongName == "-")
  96.                     {
  97.                         try
  98.                         {
  99.                             logListBox.Items.Add("Файл " + Path.GetFileName(mp3) + " не содержит тегов :( Не переименовываю!");
  100.                         }
  101.                         catch { }
  102.                     }
  103.                     else
  104.                     {
  105.                         rename(mp3, newSongName);
  106.                     }
  107.                 }
  108.             }
  109.             musicsPathListBox.Items.Clear();
  110.             goBtn.Enabled = true;
  111.             addBtn.Enabled = true;
  112.             musicsPathListBox.Enabled = true;
  113.             MessageBox.Show("Вся работа закончена :)", "Ура!", MessageBoxButtons.OK, MessageBoxIcon.Information);
  114.         }
  115.  
  116.         private String toUtf8(string unknown)
  117.         {
  118.             if (unknown != null)
  119.             {
  120.                 return new string(unknown.ToCharArray().Select(x => ((x + 848) >= 'А' && (x + 848) <= 'ё') ? (char)(x + 848) : x).ToArray());
  121.             }
  122.             else
  123.             {
  124.                 return "";
  125.             }
  126.         }
  127.  
  128.         private void clearLogBtn_Click(object sender, EventArgs e)
  129.         {
  130.             logListBox.Items.Clear();
  131.         }
  132.  
  133.         private void clearMusicList_Click(object sender, EventArgs e)
  134.         {
  135.             musicsPathListBox.Items.Clear();
  136.         }
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement