123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- using System;
- using System.IO;
- using System.Linq;
- using System.Net.Mime;
- using System.Reflection;
- using System.Text;
- using TagLib;
- using File = System.IO.File;
- // ReSharper disable InconsistentNaming
- namespace Id3TagBatchProcess
- {
- internal class Program
- {
- private static void Main(string[] args)
- {
- Console.WriteLine("Id3 Tag Batch Procesor");
- Console.WriteLine();
- try
- {
- RealMain(args);
- }
- catch (Exception e)
- {
- Console.WriteLine($"ERROR:{e.Message}");
- }
- Console.WriteLine();
- Console.Write("Press ENTER to exit...");
- Console.ReadLine();
- }
- private const string OP_DIR_EMB_COVER_MP3 = "embcover_mp3_jpg_dir";
- private const string OP_DIR_EMB_COVER_M4A = "embcover_m4a_jpg_dir";
- private const string OP_EMB_COVER = "embcover";
- private const string OP_EMB_LRC = "emblrc";
- private static void RealMain(string[] args)
- {
- if (0 == args.Length)
- {
- Console.WriteLine($"Useage: {Path.GetFileName(Assembly.GetExecutingAssembly().CodeBase)} <op> [args...]");
- Console.WriteLine($"Available ops:");
- Console.WriteLine($" * {OP_EMB_LRC} <inputPath> <lrcPath> <outputPath>");
- Console.WriteLine($" Embed lyrics into file(utf8)");
- Console.WriteLine($" * {OP_EMB_COVER} <inputPath> <coverPath> <outputPath> [mime-type]");
- Console.WriteLine($" Embed cover into file, default mine-type is {MediaTypeNames.Image.Jpeg}");
- Console.WriteLine($" * {OP_DIR_EMB_COVER_MP3} <inputDir> <outputDir> -- lookup dir, embed same file name jpg/jpeg into mp3");
- Console.WriteLine($" Lookup dir, embed same file name jpg/jpeg into mp3");
- Console.WriteLine($" Embed abc.jpg or abc.jpeg into abc.mp3 and save a copy to output dir.");
- Console.WriteLine($" * {OP_DIR_EMB_COVER_M4A} <inputDir> <outputDir> -- lookup dir, embed same file name jpg/jpeg into m4a");
- Console.WriteLine($" Lookup dir, embed same file name jpg/jpeg into m4a");
- Console.WriteLine($" Embed abc.jpg or abc.jpeg into abc.m4a and save a copy to output dir.");
- Console.WriteLine($"");
- Console.WriteLine($" + More on demand");
- return;
- }
- var op = args[0];
- var argsToOp = args.Skip(1).ToArray();
- switch (op)
- {
- default: throw new ArgumentException($"No recognizable op:{op}");
- case OP_DIR_EMB_COVER_MP3: EmbJpegCoverDir(argsToOp, "*.mp3"); break;
- case OP_DIR_EMB_COVER_M4A: EmbJpegCoverDir(argsToOp, "*.m4a"); break;
- case OP_EMB_COVER: EmbCover(argsToOp); break;
- case OP_EMB_LRC: EmbLrc(argsToOp); break;
- }
- }
- private static void EmbCover(string[] args)
- {
- if (args.Length < 3) throw new ArgumentException("args need 3");
- var pathIn = args[0];
- var pathCover = args[1];
- var pathOutput = args[2];
- var mimeType = args.Length > 3 ? args[4] : MediaTypeNames.Image.Jpeg;
- if (false == File.Exists(pathIn)) throw new DirectoryNotFoundException($"inputPath `{pathIn}' not found.");
- if (false == File.Exists(pathIn)) throw new DirectoryNotFoundException($"coverPath `{pathCover}' not found.");
- Console.WriteLine($"Processing {pathIn}...");
- Console.WriteLine($"Comver image:{pathCover}");
- File.Copy(pathIn, pathOutput);
- EmbedConverIntoFile(pathOutput, pathCover, mimeType);
- // 12345678901234
- Console.WriteLine($"Saved to: {pathOutput}");
- }
- private static void EmbLrc(string[] args)
- {
- if (args.Length < 3) throw new ArgumentException("args need 2");
- var pathIn = args[0];
- var pathLrc = args[1];
- var pathOutput = args[2];
- if (false == File.Exists(pathIn)) throw new DirectoryNotFoundException($"inputPath `{pathIn}' not found.");
- if (false == File.Exists(pathIn)) throw new DirectoryNotFoundException($"coverPath `{pathLrc}' not found.");
- Console.WriteLine($"Processing {pathIn}...");
- Console.WriteLine($"Comver image:{pathLrc}");
- File.Copy(pathIn, pathOutput);
- EmbedLrcIntoFile(pathOutput, pathLrc);
- // 12345678901234
- Console.WriteLine($"Saved to: {pathOutput}");
- }
- // impl
- private static void EmbJpegCoverDir(string[] args, string pattern)
- {
- if (args.Length != 2) throw new ArgumentException("args need 2");
- var dirIn = args[0];
- var dirOut = args[1];
- if (false == Directory.Exists(dirIn)) throw new DirectoryNotFoundException($"inputDir `{dirIn}' not found.");
- if (false == Directory.Exists(dirOut)) throw new DirectoryNotFoundException($"outputDir `{dirOut} not found.'");
- Console.WriteLine($"Enter directory:{dirIn}");
- var files = Directory.GetFiles(dirIn, pattern);
- if (0 == files.Length)
- {
- Console.WriteLine($"Not result for {pattern}");
- }
- else
- {
- foreach (var file in files)
- {
- // 12345678901234
- Console.WriteLine($"Processing {file}...");
- var imgFilePath = Path.ChangeExtension(file, ".jpg");
- if (false == File.Exists(imgFilePath)) imgFilePath = Path.ChangeExtension(file, ".jpeg");
- if (false == File.Exists(imgFilePath))
- {
- Console.WriteLine("Cover image file not found, Skipped. ***");
- continue;
- }
- // 12345678901234
- Console.WriteLine($"Comver image:{imgFilePath}");
- var destFileName = Path.Combine(dirOut, Path.GetFileName(file));
- File.Copy(file, destFileName);
- EmbedConverIntoFile(destFileName, imgFilePath, MediaTypeNames.Image.Jpeg);
- // 12345678901234
- Console.WriteLine($"Saved to: {destFileName}");
- }
- }
- Console.WriteLine($"Leave directory:{dirIn}");
- }
- private static void EmbedConverIntoFile(string filePath, string coverPath, string mimeType)
- {
- var mediaFile = TagLib.File.Create(filePath);
- var pic = new Picture //REF: stackoverflow.com/a/30285220/2430943
- {
- Type = PictureType.FrontCover,
- Description = "Cover",
- MimeType = mimeType,
- Data = File.ReadAllBytes(coverPath)
- };
- mediaFile.Tag.Pictures = new IPicture[] { pic };
- mediaFile.Save();
- }
- private static void EmbedLrcIntoFile(string filePath, string lrcPath)
- {
- var mediaFile = TagLib.File.Create(filePath);
- mediaFile.Tag.Lyrics = File.ReadAllText(lrcPath,Encoding.UTF8);
- mediaFile.Save();
- }
- }
- }
|