-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathID3.au3
1575 lines (1410 loc) · 57.8 KB
/
ID3.au3
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include-once
#include <Array.au3>
#include <String.au3>
#include <File.au3>
Dim $ID3Filenames = "",$AlbumArtFilename = ""
Dim $ID3BufferArray[1] = [0];use to buffer ID3 Data
;===============================================================================
; Function Name: _ID3ReadTag($Filename, $iVersion = 0, $sFilter = -1, $iReturnArray = 0)
; Description: Reads ID3 Tag data from mp3 file and stores tag data in an array
; Reads ID3v1.0, ID3v1.1, ID3v2.3, ID3v2.2($sFilter will not work for ID3v2.2)
; Parameter(s): $Filename - Filename of mp3 file include full path
; $iVersion - ID3 Version to read (Default: 0 => ID3v1 & ID3v2)
; 0 => Read ID3v1 & ID3v2
; 1 => Read ID3v1
; 2 => Read ID3v2
; $sFilter - ID3v2 Frame Filter (Default: -1 => all frames)
; If used format should be a pipe delimted string
; of the Frame IDs (ie. "TIT2|TALB|TPE1|TYER|APIC")
; $iReturnArray - Return Array of Field Data (Default: 0 => array not returned)
; 0 => Do not return Array
; 1 => Return Array
; Requirement(s): None
; Return Value(s): On Success - Returns the Array of tag fields and data or 1 depends on $iReturnArray.
; On Failure - 0 and Sets @error = -1 for end-of-file, @error = 1 for other errors
;Notes: (Frame ID Definitions - not all may work)
;~ AENC Audio encryption
;~ APIC Attached picture
;~ COMM Comments
;~ COMR Commercial frame
;~ ENCR Encryption method registration
;~ EQUA Equalization
;~ ETCO Event timing codes
;~ GEOB General encapsulated object
;~ GRID Group identification registration
;~ IPLS Involved people list
;~ LINK Linked information
;~ MCDI Music CD identifier
;~ MLLT MPEG location lookup table
;~ OWNE Ownership frame
;~ PRIV Private frame
;~ PCNT Play counter
;~ POPM Popularimeter
;~ POSS Position synchronisation frame
;~ RBUF Recommended buffer size
;~ RVAD Relative volume adjustment
;~ RVRB Reverb
;~ SYLT Synchronized lyric/text
;~ SYTC Synchronized tempo codes
;~ TALB Album/Movie/Show title
;~ TBPM BPM (beats per minute)
;~ TCOM Composer
;~ TCON Content type
;~ TCOP Copyright message
;~ TDAT Date
;~ TDLY Playlist delay
;~ TENC Encoded by
;~ TEXT Lyricist/Text writer
;~ TFLT File type
;~ TIME Time
;~ TIT1 Content group description
;~ TIT2 Title/songname/content description
;~ TIT3 Subtitle/Description refinement
;~ TKEY Initial key
;~ TLAN Language(s)
;~ TLEN Length
;~ TMED Media type
;~ TOAL Original album/movie/show title
;~ TOFN Original filename
;~ TOLY Original lyricist(s)/text writer(s)
;~ TOPE Original artist(s)/performer(s)
;~ TORY Original release year
;~ TOWN File owner/licensee
;~ TPE1 Lead performer(s)/Soloist(s)
;~ TPE2 Band/orchestra/accompaniment
;~ TPE3 Conductor/performer refinement
;~ TPE4 Interpreted, remixed, or otherwise modified by
;~ TPOS Part of a set
;~ TPUB Publisher
;~ TRCK Track number/Position in set
;~ TRDA Recording dates
;~ TRSN Internet radio station name
;~ TRSO Internet radio station owner
;~ TSIZ Size
;~ TSRC ISRC (international standard recording code)
;~ TSSE Software/Hardware and settings used for encoding
;~ TYER Year
;~ TXXX User defined text information frame
;~ UFID Unique file identifier
;~ USER Terms of use
;~ USLT Unsychronized lyric/text transcription
;~ WCOM Commercial information
;~ WCOP Copyright/Legal information
;~ WOAF Official audio file webpage
;~ WOAR Official artist/performer webpage
;~ WOAS Official audio source webpage
;~ WORS Official internet radio station homepage
;~ WPAY Payment
;~ WPUB Publishers official webpage
;~ WXXX User defined URL link frame
;===============================================================================
Func _ID3ReadTag($Filename, $iVersion = 0, $sFilter = -1, $iReturnArray = 0)
If Not(FileExists($Filename)) Then Return 0
Dim $ID3BufferArray[1] = [0] ;clear Buffer
Switch $iVersion
Case 0
_ReadID3v2($Filename,$ID3BufferArray,$sFilter)
_ReadID3v1($Filename,$ID3BufferArray)
Case 1
_ReadID3v1($Filename,$ID3BufferArray)
Case 2
_ReadID3v2($Filename,$ID3BufferArray,$sFilter)
EndSwitch
;Add Filename to buffer
;****************************************************************************************
_ArrayAdd($ID3BufferArray,'Filename' & '|' & $Filename)
$ID3BufferArray[0] += 1
SetError(0)
;****************************************************************************************
;Setup simple array to return (IF $iReturnArray = 1)
;****************************************************************************************
If $iReturnArray = 1 Then
;Return a 1D array with just frame string data (original)
Local $ID3Array[1] = [0]
For $i = 3 To $ID3BufferArray[0]
$ID3FrameArray = StringSplit($ID3BufferArray[$i], "|")
_ArrayAdd($ID3Array,$ID3FrameArray[1] & "|" & _ID3GetTagField($ID3FrameArray[1]))
$ID3Array[0] += 1
Next
Return $ID3Array
Elseif $iReturnArray = 2 Then
;Return a 2D array with a fields of the frames
Local $ID3FullArray[$ID3BufferArray[0]][5]
For $i = 1 To $ID3BufferArray[0]
$ID3FrameArray = StringSplit($ID3BufferArray[$i], "|")
$ID3FullArray[$i-1][0] = $ID3FrameArray[1]
$ID3FullArray[$i-1][1] = $ID3FrameArray[2]
;_ArrayAdd($ID3FullArray,$ID3FrameArray[1] & "|" & _ID3GetTagField($ID3FrameArray[1]))
;$ID3FullArray[0] += 1
Next
Return $ID3FullArray
;Return $ID3BufferArray
Else
Return 1
EndIf
;****************************************************************************************
EndFunc
;===============================================================================
; Function Name: _ID3GetTagField()
; Description: Reads the ID3 Tag Data from an mp3 file and stores it in a Buffer and returns the Field Requested
; Parameter(s): $Filename - Filename of mp3 file include full path
; $sFieldIDRequest - ID3 Field ID String of the Field to return (ie. "TIT2" for ID3v2 Title or "Title" for ID3v1 Title)
; Requirement(s): None
; Return Value(s): On Success - Returns Field String. If multiple fields found string is delimited with @CRLF.
; @error = 0; @extended = number of fields found (ID3v2 can have multiple COMM Fields)
; On Failure - Returns Empty String meaning $sFieldIDRequest did not match any IDs in the mp3 File
; @error = 1; @extended = 0
;===============================================================================
Func _ID3GetTagField($sFieldIDRequest) ;add support for ID3v2.2
;Check Buffer; If buffer is empty then set error and return
If $ID3BufferArray[0] == 0 Then
SetError(1)
Return ""
EndIf
Local $sFieldID, $sFieldString = "", $TagFound = False, $NumFound = 0
;get specified field from buffer
For $i = 1 To $ID3BufferArray[0]
$aBufferFrameString = StringSplit($ID3BufferArray[$i],"|")
If $aBufferFrameString[0] > 1 Then
$sFieldID = $aBufferFrameString[1]
If $sFieldID == $sFieldIDRequest Then
$TagFound = True
$sFieldString = _GetID3FrameString($ID3BufferArray[$i])
;~ If $TagFound Then
;~ $sFieldString = $sFieldString & @CRLF & $aBufferFrameString[2]
;~ Else
;~ $sFieldString = $aBufferFrameString[2]
;~ EndIf
;$NumFound += 1
;$TagFound = True
EndIf
EndIf
Next
;Set Error and Extended Macros
SetError(Not($TagFound),$NumFound)
Return $sFieldString
EndFunc
;updates the field in the buffer and updates the Frame Size in buffer
Func _ID3SetTagField($sFieldIDRequest, $sFieldValue)
Local $FrameID = ""
Local $FrameIDFound = False, $TagSize = 0
Local $ArrayIndex = 0, $TagSizeIndex = 0
;Get specified field from buffer
;****************************************************************************************
For $i = 1 To $ID3BufferArray[0]
$aBufferFrameString = StringSplit($ID3BufferArray[$i],"|")
If $aBufferFrameString[0] > 1 Then
$FrameID = $aBufferFrameString[1]
If $FrameID == "TagSize" Then
$TagSize = Number($aBufferFrameString[2])
$TagSizeIndex = $i
EndIf
If StringCompare($FrameID, $sFieldIDRequest,1) == 0 Then
$FrameIDFound = True
$ArrayIndex = $i
ExitLoop
EndIf
EndIf
Next
;MsgBox(262144,"$FrameIDFound",$FrameID & " - " & $FrameIDFound)
;****************************************************************************************
;Change Frame Data and Size (Need to add all Frame Types)
;****************************************************************************************
If Not $FrameIDFound Then
; this adds a new field with empty data, the data and ID are then added below
_ArrayAdd($ID3BufferArray,"")
$ID3BufferArray[0] += 1
$ArrayIndex = $ID3BufferArray[0]
;MsgBox(262144,"New Field",$FrameID & " - " & $sFieldIDRequest
$FrameID = $sFieldIDRequest
EndIf
If (StringMid($FrameID,1,1) == "T") and (StringLen($FrameID) == 4) and ($FrameID <> "TXXX") Then
;~ <Header for 'Text information frame', ID: "T000" - "TZZZ",
;~ excluding "TXXX" described in 4.2.2.>
;~ Text encoding $xx
;~ Information <text string according to encoding>
$bFrameData = Binary("0x00") ;Text Encoding
$bFrameData &= StringToBinary($sFieldValue)
$ID3BufferArray[$ArrayIndex] = $FrameID & "|" & $bFrameData & "|" & BinaryLen($bFrameData)
Elseif $FrameID == "TXXX" Then
;~ <Header for 'User defined text information frame', ID: "TXXX">
;~ Text encoding $xx
;~ Description <text string according to encoding> $00 (00)
;~ Value <text string according to encoding>
$bFrameData = Binary("0x00") ;Text Encoding
$bFrameData = Binary("0x00") ;Description
$bFrameData &= StringToBinary($sFieldValue)
$ID3BufferArray[$ArrayIndex] = $FrameID & "|" & $bFrameData & "|" & BinaryLen($bFrameData)
ElseIf (StringMid($FrameID,1,1) == "W") and (StringLen($FrameID) == 4) and ($FrameID <> "WXXX") Then
;~ <Header for 'URL link frame', ID: "W000" - "WZZZ", excluding "WXXX"
;~ described in 4.3.2.>
;~ URL <text string>
$bFrameData = StringToBinary($sFieldValue)
$ID3BufferArray[$ArrayIndex] = $FrameID & "|" & $bFrameData & "|" & BinaryLen($bFrameData)
ElseIf $FrameID == "WXXX" Then
;~ <Header for 'User defined URL link frame', ID: "WXXX">
;~ Text encoding $xx
;~ Description <text string according to encoding> $00 (00)
;~ URL <text string>
$bFrameData = Binary("0x00") ;Text Encoding
$bFrameData = Binary("0x00") ;Description
$bFrameData &= StringToBinary($sFieldValue)
$ID3BufferArray[$ArrayIndex] = $FrameID & "|" & $bFrameData & "|" & BinaryLen($bFrameData)
Else
Switch $FrameID
Case "COMM"
;~ <Header for 'Comment', ID: "COMM">
;~ Text encoding $xx
;~ Language $xx xx xx
;~ Short content descrip. <text string according to encoding> $00 (00)
;~ The actual text <full text string according to encoding>
$bFrameData = Binary("0x00") ;Text Encoding
$bFrameData &= StringToBinary("eng") ;Language (eng ONLY)
$bFrameData &= Binary("0x00") ;Short content descrip. (NONE)
if isbinary($sFieldValue) then
$bFrameData &= $sFieldValue
Else
$bFrameData &= StringToBinary($sFieldValue)
EndIf
$ID3BufferArray[$ArrayIndex] = $FrameID & "|" & $bFrameData & "|" & BinaryLen($bFrameData)
Case "APIC"
;~ <Header for 'Attached picture', ID: "APIC">
;~ Text encoding $xx
;~ MIME type <text string> $00
;~ Picture type $xx
;~ Description <text string according to encoding> $00 (00)
;~ Picture data <binary data>
$bFrameData = Binary("0x00") ;Text Encoding
If StringRight($sFieldValue,3) = "png" Then
$bFrameData &= StringToBinary("image/png") & Binary("0x00")
Else
$bFrameData &= StringToBinary("image/jpg") & Binary("0x00") ;MIME type (JPEG ONLY)
EndIf
$bFrameData &= Binary("0x03") ;Picture type ($03 Cover (front) ONLY)
$bFrameData &= StringToBinary("Cover Image") & Binary("0x00") ;Description
$FrameSize = FileGetSize($sFieldValue) + BinaryLen($bFrameData)
$ID3BufferArray[$ArrayIndex] = $FrameID & "|" & $bFrameData & "|" & $sFieldValue & "|" & $FrameSize
Case "USLT"
;~ <Header for 'Unsynchronised lyrics/text transcription', ID: "USLT">
;~ Text encoding $xx
;~ Language $xx xx xx
;~ Content descriptor <text string according to encoding> $00 (00)
;~ Lyrics/text <full text string according to encoding>
$bFrameData = Binary("0x00") ;Text Encoding
$bFrameData &= StringToBinary("eng") ;Language (eng ONLY)
$bFrameData &= Binary("0x00") ;Content descriptor (NONE)
$FrameSize = FileGetSize($sFieldValue) + BinaryLen($bFrameData)
$ID3BufferArray[$ArrayIndex] = $FrameID & "|" & $bFrameData & "|" & $sFieldValue & "|" & $FrameSize
Case "SYLT"
;~ <Header for 'Synchronised lyrics/text', ID: "SYLT">
;~ Text encoding $xx
;~ Language $xx xx xx
;~ Time stamp format $xx
;~ Content type $xx
;~ Content descriptor <text string according to encoding> $00 (00)
;~ Lyrics/text <full text string according to encoding>
$bFrameData = Binary("0x00") ;Text Encoding
$bFrameData &= StringToBinary("eng") ;Language (eng ONLY)
$bFrameData &= Binary("0x02") ;stamp format - milliseconds
$bFrameData &= Binary("0x01") ;lyrics
$bFrameData &= Binary("0x00") ;Content descriptor (NONE)
$FrameSize = FileGetSize($sFieldValue) + BinaryLen($bFrameData)
$ID3BufferArray[$ArrayIndex] = $FrameID & "|" & $bFrameData & "|" & $sFieldValue & "|" & $FrameSize
Case "UFID" ;Not Implimented Yet
;~ <Header for 'Unique file identifier', ID: "UFID">
;~ Owner identifier <text string> $00
;~ Identifier <up to 64 bytes binary data>
Case "Title" Or "Artist" Or "Album" Or "Year" or "Track" Or "Comment" or "Genre"
$ID3BufferArray[$ArrayIndex] = $FrameID & "|" & $sFieldValue
EndSwitch
EndIf
;****************************************************************************************
EndFunc
Func _ID3RemoveField($sFieldIDRequest)
Local $FrameID = ""
Local $FrameIDFound = False, $TagSize = 0
Local $ArrayIndex = 0, $TagSizeIndex = 0
;Get specified field from buffer
;****************************************************************************************
For $i = 1 To $ID3BufferArray[0]
$aBufferFrameString = StringSplit($ID3BufferArray[$i],"|")
If $aBufferFrameString[0] > 1 Then
$FrameID = $aBufferFrameString[1]
If $FrameID == "TagSize" Then
$TagSize = Number($aBufferFrameString[2])
$TagSizeIndex = $i
EndIf
If $FrameID == $sFieldIDRequest Then
$FrameIDFound = True
$ArrayIndex = $i
ExitLoop
EndIf
EndIf
Next
;****************************************************************************************
If $FrameIDFound Then
_ArrayDelete($ID3BufferArray,$ArrayIndex)
$ID3BufferArray[0] -= 1
EndIf
EndFunc
;actually writes the tag
Func _ID3WriteTag($Filename,$iFlag = 0)
Dim $ZPAD = Binary("0x00")
Local $TagFile = StringTrimRight($Filename,4) & "_ID3TAG.mp3"
If Not _FileCreate($TagFile) Then Return MsgBox(16,'错误','无法创建临时文件')
Local $OldTagSize = 0, $ID3v1Tag = StringToBinary("TAG"), $ID3v1_Artist = ""
Local $ID3v1_Title = "",$ID3v1_Album = "",$ID3v1_Year = "",$ID3v1_Comment = ""
Local $ID3v1_Genre = "", $ID3v1_Track = ""
Local $hfile,$hTagFile
$hTagFile = Fileopen($TagFile,2) ;erase all
Switch $iFlag
Case 0 ;Wite both ID3v1 and ID3v2 Tags
;Write ID3
FileWrite($hTagFile,StringToBinary("ID3"))
;Write Version (Assume ID3v2.3)
;****************************************************************************************
FileWrite($hTagFile,Binary("0x" & Hex(Number(3),2)))
FileWrite($hTagFile,Binary("0x" & Hex(Number(0),2)))
;****************************************************************************************
;Write Flags (Assume all zero)
;****************************************************************************************
FileWrite($hTagFile,Binary("0x00"))
;****************************************************************************************
;Calculate new TagSize
;****************************************************************************************
Dim $NewTagSize = 0
For $i = 1 To $ID3BufferArray[0]
$aBufferFrameString = StringSplit($ID3BufferArray[$i],"|")
If $aBufferFrameString[0] > 2 Then
$NewTagSize += Number($aBufferFrameString[$aBufferFrameString[0]]) + 10
EndIf
If $aBufferFrameString[1] == "ZPAD" Then
$NewTagSize += Number($aBufferFrameString[$aBufferFrameString[0]])
EndIf
Next
;~ MsgBox(262144,"$NewTagSize",$NewTagSize)
;****************************************************************************************
;Write TagSize (4 byte number)
;****************************************************************************************
$sTagSize = $NewTagSize
$iTagSize = Hex($NewTagSize)
$bTagSize = _HexToBin_ID3($iTagSize)
$bTagSize = _StringReverse($bTagSize)
$bTagSize = StringLeft($bTagSize,28)
$TagHeaderBin = StringMid($bTagSize,1,7) & "0" & StringMid($bTagSize,8,7) & "0" & _
StringMid($bTagSize,15,7) & "0" & StringMid($bTagSize,22,7) & "0"
$TagHeaderBin = _StringReverse($TagHeaderBin)
$TagHeader = _BinToHex_ID3($TagHeaderBin)
FileWrite($hTagFile,Binary("0x" & $TagHeader))
;****************************************************************************************
;Write Frames to File
;****************************************************************************************
For $i = 1 To $ID3BufferArray[0]
$aBufferFrameString = StringSplit($ID3BufferArray[$i],"|")
;MsgBox(262144,"$aBufferFrameString[0]",$aBufferFrameString[0])
If $aBufferFrameString[1] == "ZPAD" Then
For $iZPAD = 2 to Number($aBufferFrameString[$aBufferFrameString[0]])
$ZPAD &= Binary("0x00")
Next
$NewTagSize += Number($aBufferFrameString[$aBufferFrameString[0]])
ElseIf $aBufferFrameString[1] == "TagSize" Then
$OldTagSize = Number($aBufferFrameString[2])
;MsgBox(262144,"$OldTagSize",$OldTagSize)
ElseIf $aBufferFrameString[1] == "Title" Then
$ID3v1_Title = StringToBinary(StringLeft($aBufferFrameString[2],30))
for $iPAD = 1 to (30 - BinaryLen($ID3v1_Title))
$ID3v1_Title &= Binary("0x00")
Next
ElseIf $aBufferFrameString[1] == "Artist" Then
$ID3v1_Artist = StringToBinary(StringLeft($aBufferFrameString[2],30))
for $iPAD = 1 to (30 - BinaryLen($ID3v1_Artist))
$ID3v1_Artist &= Binary("0x00")
Next
ElseIf $aBufferFrameString[1] == "Album" Then
$ID3v1_Album = StringToBinary(StringLeft($aBufferFrameString[2],30))
for $iPAD = 1 to (30 - BinaryLen($ID3v1_Album))
$ID3v1_Album &= Binary("0x00")
Next
ElseIf $aBufferFrameString[1] == "Year" Then
$ID3v1_Year = StringToBinary(StringLeft($aBufferFrameString[2],4))
ElseIf $aBufferFrameString[1] == "Comment" Then
;MsgBox(262144,"$aBufferFrameString[2]",$aBufferFrameString[2])
$ID3v1_Comment = StringToBinary(StringLeft($aBufferFrameString[2],28)) ;28 bytes for ID3v1.1
for $iPAD = 1 to (28 - BinaryLen($ID3v1_Comment))
$ID3v1_Comment &= Binary("0x00")
Next
ElseIf $aBufferFrameString[1] == "Track" Then
$ID3v1_Track = StringLeft($aBufferFrameString[2],3)
ElseIf $aBufferFrameString[1] == "Genre" Then
$ID3v1_Genre = $aBufferFrameString[2]
EndIf
If $aBufferFrameString[0] > 2 Then
If $aBufferFrameString[1] == "APIC" Then
FileWrite($hTagFile,StringToBinary($aBufferFrameString[1]))
FileWrite($hTagFile,Binary("0x" & Hex($aBufferFrameString[$aBufferFrameString[0]],8))) ;FrameSize
FileWrite($hTagFile,Binary("0x" & Hex(0,2))) ;FrameFlag1
FileWrite($hTagFile,Binary("0x" & Hex(0,2))) ;FrameFlag2
FileWrite($hTagFile,Binary($aBufferFrameString[2]))
$PicFile_h = FileOpen($aBufferFrameString[3], 16) ;force binary
$WriteError = FileWrite($hTagFile,FileRead($PicFile_h))
FileClose($PicFile_h)
ElseIf $aBufferFrameString[1] == "USLT" Or $aBufferFrameString[1] == "SYLT" Then
FileWrite($hTagFile,StringToBinary($aBufferFrameString[1]))
FileWrite($hTagFile,Binary("0x" & Hex($aBufferFrameString[$aBufferFrameString[0]],8))) ;FrameSize
FileWrite($hTagFile,Binary("0x" & Hex(0,2))) ;FrameFlag1
FileWrite($hTagFile,Binary("0x" & Hex(0,2))) ;FrameFlag2
FileWrite($hTagFile,Binary($aBufferFrameString[2]))
$LyricsFile_h = FileOpen($aBufferFrameString[3], 16) ;force binary
$LyricData = FileRead($LyricsFile_h)
$WriteError = FileWrite($hTagFile,$LyricData)
FileClose($LyricsFile_h)
Else
FileWrite($hTagFile,StringToBinary($aBufferFrameString[1]))
FileWrite($hTagFile,Binary("0x" & Hex($aBufferFrameString[$aBufferFrameString[0]],8))) ;FrameSize
FileWrite($hTagFile,Binary("0x" & Hex(0,2))) ;FrameFlag1
FileWrite($hTagFile,Binary("0x" & Hex(0,2))) ;FrameFlag2
FileWrite($hTagFile,Binary($aBufferFrameString[2])) ;Frame Data
EndIf
$NewTagSize += Number($aBufferFrameString[$aBufferFrameString[0]]) + 10
EndIf
Next
;****************************************************************************************
;Write Tag Padding
;****************************************************************************************
FileWrite($hTagFile,$ZPAD)
;****************************************************************************************
;Write MP3 Data
;****************************************************************************************
$hFile = FileOpen($Filename,16) ;read force binary
;~ FileRead($hFile,$OldTagSize) ;read past Old Tag
FileSetPos ($hFile, $OldTagSize, $FILE_BEGIN)
FileWrite($hTagFile,FileRead($hFile,FileGetSize($Filename) - 128 - $OldTagSize)) ;Write the mp3 data
;****************************************************************************************
;Write ID3v1.1 Tag
;****************************************************************************************
$ID3v1Tag &= $ID3v1_Title
$ID3v1Tag &= $ID3v1_Artist
$ID3v1Tag &= $ID3v1_Album
$ID3v1Tag &= $ID3v1_Year
$ID3v1Tag &= $ID3v1_Comment
$ID3v1Tag &= Binary("0x00")
$ID3v1Tag &= Binary("0x" & Hex(Number($ID3v1_Track),2))
$ID3v1Tag &= Binary("0x" & Hex(_GetGenreID($ID3v1_Genre),2))
;check tag as proper length
;MsgBox(262144,"ID3v1 Tag Length = ", BinaryLen($ID3v1Tag))
FileWrite($hTagFile,$ID3v1Tag)
;****************************************************************************************
Case 1
Dim $ID3BufferArray[1] = [0]
$OldTagSize=_ReadID3v2($Filename,$ID3BufferArray,-1,True)
$hFile = FileOpen($Filename,16) ;read force binary
FileSetPos ($hFile, $OldTagSize, $FILE_BEGIN)
;~ FileRead($hFile,$OldTagSize) ;read past Old Tag
FileWrite($hTagFile,FileRead($hFile,FileGetSize($Filename) - 128 - $OldTagSize)) ;Write the mp3 data
$ID3v1Tag=FileRead($hFile)
If Not (BinaryToString(BinaryMid($ID3v1Tag,1,3)) == 'TAG') Then FileWrite($hTagFile,$ID3v1Tag)
EndSwitch
FileClose($hFile)
FileClose($hTagFile)
;~ If not FileDelete($Filename) Then MsgBox(262144,'','无法删除源文件!')
If not FileMove($TagFile,$Filename,1) Then Return MsgBox(262144,'','无法覆盖原文件或覆盖时出现错误!')
Return 1
EndFunc
;===============================================================================
; Function Name: _ID3DeleteFiles()
; Description: Deletes any files created by ID3.au3 (ie. AlbumArt.jpeg and SongLyrics.txt)
; Parameter(s): None
; Requirement(s): None
; Return Value(s): On Success - Returns 1.
; On Failure - Returns 0
;===============================================================================
Func _ID3DeleteFiles()
If $ID3Filenames == "" Then Return 1
$aID3File = StringSplit($ID3Filenames,"|")
For $i = 1 To $aID3File[0]
If FileExists($aID3File[$i]) Then
$ret = FileDelete($aID3File[$i])
If $ret == 0 Then Return 0
EndIf
Next
$ID3Filenames = ""
Return 1
EndFunc
Func _ID3GetFrameDescription($sFrameID)
Local $sFrameD = ""
Switch $sFrameID
Case "UFID"
$sFrameD = "This frame's purpose is to be able to identify the audio file in a " & _
"database that may contain more information relevant to the content. " & _
"Since standardisation of such a database is beyond this document, all " & _
"frames begin with a null-terminated string with a URL [URL] " & _
"containing an email address, or a link to a location where an email " & _
"address can be found, that belongs to the organisation responsible " & _
"for this specific database implementation. Questions regarding the " & _
"database should be sent to the indicated email address. The URL " & _
"should not be used for the actual database queries. The string " & _
'"http://www.id3.org/dummy/ufid.html" should be used for tests. ' & _
"Software that isn't told otherwise may safely remove such frames. The " & _
"'Owner identifier' must be non-empty (more than just a termination). " & _
"The 'Owner identifier' is then followed by the actual identifier, " & _
'which may be up to 64 bytes. There may be more than one "UFID" frame ' & _
"in a tag, but only one with the same 'Owner identifier'."
Case "TALB"
$sFrameD = "The 'Album/Movie/Show title' frame is intended for the title of the " & _
"recording(/source of sound) which the audio in the file is taken " & _
"from."
Case "TBPM"
$sFrameD = "The 'BPM' frame contains the number of beats per minute in the " & _
"mainpart of the audio. The BPM is an integer and represented as a " & _
"numerical string."
EndSwitch
Return $sFrameD
EndFunc
; -----------------------------------------------------------------------------
;------------------- Main File Reading Functions -----------------------------
Func _ReadID3v2($Filename, ByRef $aID3V2Tag, $sFilter = -1, $HeadOnly = False)
Local $ZPAD = 0, $BytesRead = 0
Local $hFile = FileOpen($Filename,16) ;mode = Force binary
Local $iFilterNum
If $sFilter <> -1 Then
$aFilter = StringSplit($sFilter,"|")
$iFilterNum = $aFilter[0]
EndIf
;Read Header Bytes (First 10 Bytes of File)
;****************************************************************************************
Local $ID3v2Header = FileRead($hFile, 10)
_ArrayAdd($aID3V2Tag,"Header" & "|" & $ID3v2Header)
$aID3V2Tag[0] += 1
$BytesRead = 10 ;ID3 Header has been read (Header Size = 10 Bytes)
;~ MsgBox(262144,"$ID3v2Header",$ID3v2Header)
;****************************************************************************************
;Read "ID3" string
;****************************************************************************************
If Not(BinaryToString(BinaryMid($ID3v2Header,1,3)) == "ID3") Then
FileClose($hFile)
SetError(1)
;MsgBox(262144,"ID3 string not found in Header",BinaryToString(BinaryMid($ID3v2Header,1,3)))
Return 0
EndIf
;****************************************************************************************
;GetTagVer
;****************************************************************************************
Local $FrameIDLen ;equals 3 or 4 depending on ID3v2.2 or ID3v2.3+
Local $ID3v2Version = String(Number(BinaryMid($ID3v2Header,4,1))) & "." & String(Number(BinaryMid($ID3v2Header,5,1)))
If $sFilter == -1 Then
_ArrayAdd($aID3V2Tag,"Version" & "|" & "ID3v2." & $ID3v2Version)
$aID3V2Tag[0] += 1
EndIf
;~ MsgBox(262144,"$ID3v2Version",$ID3v2Version)
If StringInStr($ID3v2Version,"2.") Then
$FrameIDLen = 3
If $sFilter <> -1 Then
_ConvertFilterToID3v2_2($sFilter)
EndIf
Else
$FrameIDLen = 4
EndIf
;****************************************************************************************
;GetTagFlags
;****************************************************************************************
Local $TagFlagsBin = BinaryMid($ID3v2Header,6,1)
Local $TagFlags = _HexToBin_ID3(StringTrimLeft($TagFlagsBin,2))
Local $Unsynchronisation = StringMid($TagFlags,1,1)
Local $ExtendedHeader = StringMid($TagFlags,2,1)
Local $ExperimentalIndicator = StringMid($TagFlags,3,1)
Local $Footer = StringMid($TagFlags,4,1)
If Not $TagFlags == "00000000" Then
;MsgBox(262144,"$ID3TagFlags", $TagFlags)
EndIf
If $sFilter == -1 Then
_ArrayAdd($aID3V2Tag,"Unsynchronisation" & "|" & $Unsynchronisation)
_ArrayAdd($aID3V2Tag,"ExtendedHeader" & "|" & $ExtendedHeader)
_ArrayAdd($aID3V2Tag,"ExperimentalIndicator" & "|" & $ExperimentalIndicator)
_ArrayAdd($aID3V2Tag,"Footer" & "|" & $Footer)
$aID3V2Tag[0] += 4
EndIf
;****************************************************************************************
;GetTagSize
;****************************************************************************************
Local $TagSizeBin = ""
$TagHeaderBin = _HexToBin_ID3(StringTrimLeft(BinaryMid($ID3v2Header,7,4),2)) ;Removes 0x
For $i = 1 To 33 Step 8
$TagSizeBin &= StringMid($TagHeaderBin,$i + 1,7) ;Skip most significant bit in every byte
Next
Local $TagSize = Dec(_BinToHex_ID3($TagSizeBin)) + 10 ;add 10 to include header
If ($sFilter == -1) or StringInStr($sFilter,"TagSize") Then
_ArrayAdd($aID3V2Tag,"TagSize" & "|" & $TagSize)
$aID3V2Tag[0] += 1
If $aID3V2Tag[0] == ($iFilterNum + 1) Then ;Plus one needed to account for Header
FileClose($hFile)
Return 1
EndIf
EndIf
If $HeadOnly Then
FileClose($hFile)
Return $TagSize
EndIf
;~ MsgBox(262144,"$TagSize",$TagSize)
;****************************************************************************************
;Read in all of the tag and close the file might be faster
Local $ZPadding, $FrameIDFristHex, $FrameID, $FrameSizeHex, $FrameSize, $FrameFlag1, $FrameFlag2, $FoundTag, $index
Local $FrameHeader
;Get Rest Of Tag
While $BytesRead < $TagSize
;Initialize ZPAD & Read first Hex value to test for new Frame or NULL Bytes
;****************************************************************************************
$ZPadding = 0
$FrameIDFristHex = StringTrimLeft(FileRead($hFile,1),2)
$BytesRead += 1
;~ MsgBox(262144,"Chr(Dec($FrameIDFristHex))",Chr(Dec($FrameIDFristHex)))
;****************************************************************************************
;check for NULL
If $FrameIDFristHex == "00" Then
;Count how many NULL Bytes at the end of the Tag then Exit Loop
;****************************************************************************************
$ZPadding += 1
While $FrameIDFristHex == "00"
$FrameIDFristHex = StringTrimLeft(FileRead($hFile,1),2)
$BytesRead += 1
$ZPadding += 1
If $BytesRead >= $TagSize Then
ExitLoop
EndIf
WEnd
$ZPAD = $ZPadding
ExitLoop
;****************************************************************************************
Else
;Read Frame ID String
;****************************************************************************************
$FrameID = Chr(Dec($FrameIDFristHex)) & BinaryToString(FileRead($hFile,$FrameIDLen-1))
$BytesRead += $FrameIDLen-1
;****************************************************************************************
;Check for a valid frameID string
If StringIsAlNum($FrameID) Then
;Read Frame Header & Size (Differant for 2.2 and 2.3+)
;****************************************************************************************
If $FrameIDLen == 4 Then
$bFrameHeader = FileRead($hFile,6)
$BytesRead += 6
$FrameSizeHex = StringTrimLeft(BinaryMid($bFrameHeader,1,4),2)
$FrameSize = _HexToUint32_ID3($FrameSizeHex)
$FrameFlag1 = _HexToBin_ID3(StringTrimLeft(BinaryMid($bFrameHeader,5,1),2))
$FrameFlag2 = _HexToBin_ID3(StringTrimLeft(BinaryMid($bFrameHeader,6,1),2))
ElseIf $FrameIDLen == 3 Then
$bFrameHeader = FileRead($hFile,3) ;only frame size
$BytesRead += 3
$FrameSizeHex = StringTrimLeft(BinaryMid($bFrameHeader,1,3),2)
$FrameSize = Dec($FrameSizeHex)
EndIf
;~ MsgBox(262144,$FrameID,$FrameID)
;****************************************************************************************
;Add FrameID and Frame Data to array
;****************************************************************************************
If $sFilter == -1 Then
Switch $FrameID
Case "APIC" ;Picture (Write to File)
$FrameData = _GetAlbumArt($hFile,$FrameSize)
Case "USLT" ;Lyrics (Write to File)
$FrameData = _GetSongLyrics($hFile,$FrameSize)
Case "SYLT"
$FrameData = _GetSynLyrics($hFile,$FrameSize)
Case Else
$FrameData = FileRead($hFile,$FrameSize)
EndSwitch
_ArrayAdd($aID3V2Tag,$FrameID & "|" & $FrameData & "|" & $FrameSize)
$aID3V2Tag[0] += 1
$BytesRead += $FrameSize
Else
If $aID3V2Tag[0] == ($iFilterNum + 1) Then ;Plus one needed to account for Header
ExitLoop
EndIf
If StringInStr($sFilter,$FrameID) Then
Switch $FrameID
Case "APIC" ;Picture (Write to File)
$FrameData = _GetAlbumArt($hFile,$FrameSize)
Case "USLT" ;Lyrics (Write to File)
$FrameData = _GetSongLyrics($hFile,$FrameSize)
Case "SYLT"
$FrameData = _GetSynLyrics($hFile,$FrameSize)
Case Else
$FrameData = FileRead($hFile,$FrameSize)
EndSwitch
_ArrayAdd($aID3V2Tag,$FrameID & "|" & $FrameData & "|" & $FrameSize)
$aID3V2Tag[0] += 1
Else
FileRead($hFile,$FrameSize)
EndIf
$BytesRead += $FrameSize
EndIf
;****************************************************************************************
EndIf
EndIf
WEnd
;Read MPEG Header & Write to Array MPEG & ZPAD
;****************************************************************************************
If ($sFilter == -1) or StringInStr($sFilter,"MPEG") or StringInStr($sFilter,"ZPAD") Then
Local $MPEGHeaderCheck = $FrameIDFristHex & StringTrimLeft(FileRead($hFile,50),2)
Local $index = StringInStr($MPEGHeaderCheck,"FF")
Local $MPEGHeaderHex = StringMid($MPEGHeaderCheck,$index,8)
;check MPEG Header
If _CheckMPEGHeader($MPEGHeaderHex) and (StringInStr($sFilter,"MPEG") or ($sFilter == -1)) Then
_ArrayAdd($aID3V2Tag,"MPEG" & "|" & $MPEGHeaderHex)
$aID3V2Tag[0] += 1
EndIf
If StringInStr($sFilter,"ZPAD") or ($sFilter == -1) Then
_ArrayAdd($aID3V2Tag,"ZPAD" & "|" & $ZPAD)
$aID3V2Tag[0] += 1
EndIf
EndIf
;****************************************************************************************
FileClose($hFile)
Return 1
EndFunc
Func _ReadID3v1($Filename, ByRef $aID3V1Tag)
Local $hfile = FileOpen($Filename,16) ;open in binary mode
FileRead($hfile,FileGetSize($Filename)-128)
Local $ID3v1Tag = FileRead($hfile)
FileClose($hfile)
Local $ID3v1ID = BinaryToString(BinaryMid($ID3v1Tag,1,3))
;MsgBox(262144,"ID3v1 Tag ID",$ID3v1ID)
If Not($ID3v1ID == "TAG") Then
FileClose($hfile)
SetError(-1)
Return 0
EndIf
Local $Title, $Artist, $Album, $Year, $Comment, $Track, $GenreID, $Genre
$Title = BinaryToString(BinaryMid($ID3v1Tag,4,30))
_ArrayAdd($aID3V1Tag,"Title" & "|" & $Title)
$aID3V1Tag[0] += 1
$Artist = BinaryToString(BinaryMid($ID3v1Tag,34,30))
_ArrayAdd($aID3V1Tag,"Artist" & "|" & $Artist)
$aID3V1Tag[0] += 1
$Album = BinaryToString(BinaryMid($ID3v1Tag,64,30))
_ArrayAdd($aID3V1Tag,"Album" & "|" & $Album)
$aID3V1Tag[0] += 1
;MsgBox(262144,"$Album",$Album)
$Year = BinaryToString(BinaryMid($ID3v1Tag,94,4))
_ArrayAdd($aID3V1Tag,"Year" & "|" & $Year)
$aID3V1Tag[0] += 1
;MsgBox(262144,"$Year",$Year)
$Comment = BinaryToString(BinaryMid($ID3v1Tag,98,28))
_ArrayAdd($aID3V1Tag,"Comment" & "|" & $Comment)
$aID3V1Tag[0] += 1
;MsgBox(262144,"$Comment",$Comment)
$Track = Dec(StringTrimLeft(BinaryMid($ID3v1Tag,126,2),2))
;~ MsgBox(262144,"$Track",$Track)
If $Track < 1000 And $Track > 0 Then
_ArrayAdd($aID3V1Tag,"Track" & "|" & $Track)
$aID3V1Tag[0] += 1
Else
_ArrayAdd($aID3V1Tag,"Track" & "|" & "")
$aID3V1Tag[0] += 1
EndIf
$GenreID = Dec(StringTrimLeft(BinaryMid($ID3v1Tag,128,1),2))
$Genre = _GetGenreByID($GenreID)
_ArrayAdd($aID3V1Tag,"Genre" & "|" & $Genre)
$aID3V1Tag[0] += 1
If $Track == 0 Then
_ArrayAdd($aID3V1Tag,"Version1" & "|" & "ID3v1.0")
$aID3V1Tag[0] += 1
Else
_ArrayAdd($aID3V1Tag,"Version1" & "|" & "ID3v1.1")
$aID3V1Tag[0] += 1
EndIf
FileClose($hfile)
Return 1
EndFunc
Func _GetID3FrameString($sFrameData)
Local $bTagFieldFound = False, $sFrameString = ""
Local $bText_Encoding_Description_Byte
Local $bUnicode_BOM
Local $BinaryToString_Flag = 1 ;Default binary data is taken to be ANSI
;~ 2, binary data is taken to be UTF16 Little Endian (BOM = FF FE)
;~ 3, binary data is taken to be UTF16 Big Endian (BOM = FE FF)
;~ 4, binary data is taken to be UTF8 (BOM = EF BB BF)
$aFrameData = StringSplit($sFrameData,"|")
$FrameID = $aFrameData[1]
If (StringMid($FrameID,1,1) == "T") and (StringLen($FrameID) == 4) and ($FrameID <> "TXXX") and ($FrameID <> "TCON") Then
$bTagFieldFound = True
$bFrameData = Binary($aFrameData[2])
$bText_Encoding_Description_Byte = Int(BinaryMid($bFrameData,1,1))
;MsgBox(262144,"$bText_Encoding_Description_Byte",$bText_Encoding_Description_Byte)
If $bText_Encoding_Description_Byte <> 0 Then
$bUnicode_BOM = BinaryMid($bFrameData,2,2)
If $bUnicode_BOM = "0xFFFE" Then
$BinaryToString_Flag = 2 ;UTF16 Little Endian
EndIf
If $bUnicode_BOM = "0xFEFF" Then
$BinaryToString_Flag = 3 ;UTF16 Big Endian
EndIf
$sFrameString = BinaryToString(BinaryMid($bFrameData,4),$BinaryToString_Flag)
Else
$sFrameString = BinaryToString(BinaryMid($bFrameData,2))
EndIf
Elseif $FrameID == "TXXX" Then
;User defined text information frame
$bTagFieldFound = True
$bFrameData = Binary($aFrameData[2])
$bText_Encoding_Description_Byte = BinaryMid($bFrameData,1,1)
$ByteIndex = 2
$Description = ""
$Byte = ""
$BinaryToString_Flag = 1
If $bText_Encoding_Description_Byte <> 0 Then
If BinaryMid($bFrameData,$ByteIndex,2) = "0xFFFE" Then
$BinaryToString_Flag = 2 ;UTF16 Little Endian
$ByteIndex += 2
EndIf
If BinaryMid($bFrameData,$ByteIndex,2) = "0xFEFF" Then
$BinaryToString_Flag = 3 ;UTF16 Big Endian
$ByteIndex += 2
EndIf
EndIf
While BinaryMid($bFrameData,$ByteIndex,1) <> "0x00"
;$Short_Content_Descrip &= BinaryToString($Byte)
$Byte = binary($Byte & BinaryMid($bFrameData,$ByteIndex,1))
$ByteIndex += 1
WEnd
While BinaryMid($bFrameData,$ByteIndex,1) == "0x00"
$ByteIndex += 1
WEnd
$Description = BinaryToString($Byte,$BinaryToString_Flag)
;MsgBox(262144,"$Short_Content_Descrip",$Short_Content_Descrip)
$BinaryToString_Flag = 1
If $bText_Encoding_Description_Byte <> 0 Then
If BinaryMid($bFrameData,$ByteIndex,2) = "0xFFFE" Then
$BinaryToString_Flag = 2 ;UTF16 Little Endian
EndIf
If BinaryMid($bFrameData,$ByteIndex,2) = "0xFEFF" Then
$BinaryToString_Flag = 3 ;UTF16 Big Endian
EndIf
$ByteIndex += 2
EndIf
$sFrameString = BinaryToString(BinaryMid($bFrameData,$ByteIndex),$BinaryToString_Flag)
If $Description <> "" then
$sFrameString = $Description & "-" & $sFrameString
EndIf
;~ MsgBox(262144,"$sFrameString",$sFrameString)
Elseif $FrameID == "TCON" Then;Content Type/Genre
$bTagFieldFound = True
$bFrameData = Binary($aFrameData[2])
;~ $bFrameData = BinaryMid($bFrameData,2)