File: BatchSet.cs

package info (click to toggle)
taglib-sharp 2.1.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 3,880 kB
  • sloc: cs: 29,524; sh: 594; makefile: 229
file content (131 lines) | stat: -rw-r--r-- 3,023 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Collections.Generic;
using TagLib;

public class BatchSet
{
	private enum Mode {
		Tag, Value, File
	}
	
	public static void Main(string [] args)
	{
		if(args.Length < 3) {
			Console.Error.WriteLine ("USAGE: BatchSet.exe -tag value [-tag2 value ...] File1 [File2 ...]");
			return;
		}
		
		Mode mode = Mode.Tag;
		List<string> files = new List<string> ();
		Dictionary<string,string> tags  = new Dictionary<string,string> ();
		
		string tag = null;
		
		foreach (string str in args) {
			if (mode == Mode.Tag) {
				if (str [0] == '-') {
					if (str == "--") {
						mode = Mode.File;
					} else {
						tag = str.Substring (1);
						mode = Mode.Value;
					}
					
					continue;
				}
				mode = Mode.File;
			}
			
			if (mode == Mode.Value) {
				if (!tags.ContainsKey (tag))
					tags.Add (tag, str);
				mode = Mode.Tag;
				continue;
			}
			
			if (mode == Mode.File)
				files.Add (str);
		}
		
		foreach (string filename in files) {
			TagLib.File file = TagLib.File.Create (filename);
			if (file == null)
				continue;
			
			Console.WriteLine ("Updating Tags For: " + filename);
		
			foreach (string key in tags.Keys) {
				string value = tags [key];
				try {
					switch (key) {
					case "id3version":
						byte number = byte.Parse (value);
						if (number == 1) {
							file.RemoveTags (TagTypes.Id3v2);
						} else {
							TagLib.Id3v2.Tag v2 =
								file.GetTag (TagTypes.Id3v2, true)
								as TagLib.Id3v2.Tag;
							
							if (v2 != null)
								v2.Version = number;
						}
						break;
					case "album":
						file.Tag.Album = value;
						break;
					case "artists":
						file.Tag.AlbumArtists = value.Split (new char [] {';'});
						break;
					case "comment":
						file.Tag.Comment = value;
						break;
					case "lyrics":
						file.Tag.Lyrics = value;
						break;
					case "composers":
						file.Tag.Composers = value.Split (new char [] {';'});
						break;
					case "disc":
						file.Tag.Disc = uint.Parse (value);
						break;
					case "disccount":
						file.Tag.DiscCount = uint.Parse (value);
						break;
					case "genres":
						file.Tag.Genres = value.Split (new char [] {';'});
						break;
					case "performers":
						file.Tag.Performers = value.Split (new char [] {';'});
						break;
					case "title":
						file.Tag.Title = value;
						break;
					case "track":
						file.Tag.Track = uint.Parse (value);
						break;
					case "trackcount":
						file.Tag.TrackCount = uint.Parse (value);
						break;
					case "year":
						file.Tag.Year = uint.Parse (value);
						break;
					case "pictures":
						List<Picture> pics = new List<Picture> ();
						if (!string.IsNullOrEmpty (value))
							foreach (string path in value.Split (new char [] {';'})) {
								pics.Add (new Picture (path));
							}
						file.Tag.Pictures = pics.ToArray ();
						break;
					}
				} catch (Exception e) {
					Console.WriteLine ("Error setting tag " + key + ":");
					Console.WriteLine (e);
				}
			}
			
			file.Save();
		}
	}
}