| 1 |
{ |
| 2 |
Double commander |
| 3 |
------------------------------------------------------------------------- |
| 4 |
Class TMP4file - for manipulating with M4A audio file information |
| 5 |
|
| 6 |
Copyright (C) 2016 Alexander Koblov ([email protected]) |
| 7 |
|
| 8 |
This library is free software; you can redistribute it and/or |
| 9 |
modify it under the terms of the GNU Lesser General Public |
| 10 |
License as published by the Free Software Foundation; either |
| 11 |
version 2.1 of the License, or (at your option) any later version. |
| 12 |
|
| 13 |
This library is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
Lesser General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU Lesser General Public |
| 19 |
License along with this library; if not, write to the Free Software |
| 20 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 |
} |
| 22 |
|
| 23 |
unit MP4file; |
| 24 |
|
| 25 |
{$mode objfpc}{$H+} |
| 26 |
|
| 27 |
interface |
| 28 |
|
| 29 |
uses |
| 30 |
Classes, SysUtils, DCClassesUtf8; |
| 31 |
|
| 32 |
type |
| 33 |
TAtomName = array [0..3] of AnsiChar; |
| 34 |
|
| 35 |
{ TMP4file } |
| 36 |
|
| 37 |
TMP4file = class |
| 38 |
private |
| 39 |
FFileSize: Int64; |
| 40 |
FStream: TStream; |
| 41 |
function GetValid: Boolean; |
| 42 |
private |
| 43 |
FChannels: Word; |
| 44 |
FBitRate: Double; |
| 45 |
FDuration: Double; |
| 46 |
FSampleSize: Word; |
| 47 |
FSampleRate: LongWord; |
| 48 |
private |
| 49 |
FYear, |
| 50 |
FGenre, |
| 51 |
FTitle, |
| 52 |
FAlbum, |
| 53 |
FArtist, |
| 54 |
FEncoder, |
| 55 |
FComment, |
| 56 |
FComposer, |
| 57 |
FCopyright: String; |
| 58 |
FTrack: LongWord; |
| 59 |
protected |
| 60 |
procedure ResetData; |
| 61 |
procedure ReadMovieHeader; |
| 62 |
procedure ReadMetaDataItemList; |
| 63 |
procedure ReadSampleDescription; |
| 64 |
function ReadAtomData: String; |
| 65 |
function ReadGenreData: String; |
| 66 |
function ReadTrackData: LongWord; |
| 67 |
function FindAtomHeader(const AName: TAtomName; ASize: PInt64 = nil): Boolean; |
| 68 |
function LoadAtomHeader(out AtomName: TAtomName; out AtomSize: Int64): Boolean; |
| 69 |
public |
| 70 |
constructor Create; { Create object } |
| 71 |
destructor Destroy; override; { Destroy object } |
| 72 |
function ReadFromFile(const FileName: String): Boolean; { Load header } |
| 73 |
property FileSize: Int64 read FFileSize; { File size (bytes) } |
| 74 |
property Channels: Word read FChannels; { Number of channels } |
| 75 |
property SampleRate: LongWord read FSampleRate; { Sample rate (hz) } |
| 76 |
property BitRate: Double read FBitRate; { Bit rate (bit/s) } |
| 77 |
property Duration: Double read FDuration; { Duration (seconds) } |
| 78 |
property Year: String read FYear; { Release year } |
| 79 |
property Genre: String read FGenre; { Genre name } |
| 80 |
property Track: LongWord read FTrack; { Track number } |
| 81 |
property Title: String read FTitle; { Song title } |
| 82 |
property Album: String read FAlbum; { Album title } |
| 83 |
property Artist: String read FArtist; { Artist name } |
| 84 |
property Comment: String read FComment; { Comment } |
| 85 |
property Encoder: String read FEncoder; { Encoder } |
| 86 |
property Composer: String read FComposer; { Composer } |
| 87 |
property Copyright: String read FCopyright; { Copyright } |
| 88 |
property Valid: Boolean read GetValid; { True if data valid } |
| 89 |
end; |
| 90 |
|
| 91 |
implementation |
| 92 |
|
| 93 |
uses |
| 94 |
ID3v1; |
| 95 |
|
| 96 |
{ TMP4file } |
| 97 |
|
| 98 |
function TMP4file.FindAtomHeader(const AName: TAtomName; ASize: PInt64): Boolean; |
| 99 |
var |
| 100 |
AtomSize: Int64; |
| 101 |
APosition: Int64; |
| 102 |
AtomName: TAtomName; |
| 103 |
begin |
| 104 |
repeat |
| 105 |
if not LoadAtomHeader(AtomName, AtomSize) then |
| 106 |
Break; |
| 107 |
if SameText(AtomName, AName) then |
| 108 |
begin |
| 109 |
if Assigned(ASize) then ASize^:= AtomSize; |
| 110 |
Exit(True); |
| 111 |
end |
| 112 |
else begin |
| 113 |
APosition:= FStream.Seek(AtomSize, soCurrent); |
| 114 |
end; |
| 115 |
until (APosition >= FFileSize); |
| 116 |
Result:= False; |
| 117 |
end; |
| 118 |
|
| 119 |
function TMP4file.GetValid: Boolean; |
| 120 |
begin |
| 121 |
Result:= (FDuration > 0.0) and (FBitRate > 0.0); |
| 122 |
end; |
| 123 |
|
| 124 |
function TMP4file.LoadAtomHeader(out AtomName: TAtomName; out AtomSize: Int64): Boolean; |
| 125 |
begin |
| 126 |
AtomSize:= SwapEndian(FStream.ReadDWord); |
| 127 |
FillChar({%H-}AtomName, SizeOf(TAtomName), #0); |
| 128 |
FStream.Read(AtomName, SizeOf(TAtomName)); |
| 129 |
if AtomSize <> 1 then |
| 130 |
AtomSize:= AtomSize - 8 |
| 131 |
else begin |
| 132 |
AtomSize:= Int64(SwapEndian(FStream.ReadQWord)) - 16; |
| 133 |
end; |
| 134 |
Result:= not ((AtomSize < 0) or (AtomSize > FFileSize)); |
| 135 |
end; |
| 136 |
|
| 137 |
procedure TMP4file.ResetData; |
| 138 |
begin |
| 139 |
FTrack:= 0; |
| 140 |
FBitRate:= 0; |
| 141 |
FChannels:= 0; |
| 142 |
FDuration:= 0.0; |
| 143 |
FSampleSize:= 0; |
| 144 |
FSampleRate:= 0; |
| 145 |
FYear:= EmptyStr; |
| 146 |
FGenre:= EmptyStr; |
| 147 |
FTitle:= EmptyStr; |
| 148 |
FAlbum:= EmptyStr; |
| 149 |
FArtist:= EmptyStr; |
| 150 |
FEncoder:= EmptyStr; |
| 151 |
FComment:= EmptyStr; |
| 152 |
FComposer:= EmptyStr; |
| 153 |
FCopyright:= EmptyStr; |
| 154 |
end; |
| 155 |
|
| 156 |
procedure TMP4file.ReadMovieHeader; |
| 157 |
var |
| 158 |
AVersion: Byte; |
| 159 |
MediaSize: Int64; |
| 160 |
ADuration: QWord = 0; |
| 161 |
TimeScale: LongWord = 0; |
| 162 |
begin |
| 163 |
FStream.Seek(0, soBeginning); |
| 164 |
if FindAtomHeader('moov') and FindAtomHeader('mvhd') then |
| 165 |
begin |
| 166 |
AVersion:= FStream.ReadByte; |
| 167 |
FStream.Seek(3, soCurrent); |
| 168 |
if AVersion = 0 then |
| 169 |
begin |
| 170 |
FStream.Seek(8, soCurrent); |
| 171 |
TimeScale:= SwapEndian(FStream.ReadDWord); |
| 172 |
ADuration:= SwapEndian(FStream.ReadDWord); |
| 173 |
end |
| 174 |
else if AVersion = 1 then |
| 175 |
begin |
| 176 |
FStream.Seek(16, soCurrent); |
| 177 |
TimeScale:= SwapEndian(FStream.ReadDWord); |
| 178 |
ADuration:= SwapEndian(FStream.ReadQWord); |
| 179 |
end; |
| 180 |
if TimeScale > 0 then FDuration:= ADuration / TimeScale; |
| 181 |
end; |
| 182 |
FStream.Seek(0, soBeginning); |
| 183 |
if (FDuration > 0) and FindAtomHeader('mdat', @MediaSize) then |
| 184 |
begin |
| 185 |
FBitRate:= MediaSize * 8 / FDuration / 1000; |
| 186 |
end; |
| 187 |
end; |
| 188 |
|
| 189 |
function TMP4file.ReadAtomData: String; |
| 190 |
var |
| 191 |
AtomSize: Int64; |
| 192 |
DataType: LongWord; |
| 193 |
Buffer: array[Byte] of AnsiChar; |
| 194 |
begin |
| 195 |
Result:= EmptyStr; |
| 196 |
if FindAtomHeader('data', @AtomSize) then |
| 197 |
begin |
| 198 |
if AtomSize - 8 > High(Byte) then Exit; |
| 199 |
DataType:= SwapEndian(FStream.ReadDWord); |
| 200 |
if DataType = 1 then |
| 201 |
begin |
| 202 |
FStream.Seek(4, soCurrent); |
| 203 |
FStream.Read({%H-}Buffer, AtomSize - 8); |
| 204 |
SetString(Result, Buffer, AtomSize - 8); |
| 205 |
end; |
| 206 |
end; |
| 207 |
end; |
| 208 |
|
| 209 |
function TMP4file.ReadGenreData: String; |
| 210 |
var |
| 211 |
AtomSize: Int64; |
| 212 |
AGenre: Word = 0; |
| 213 |
begin |
| 214 |
Result:= EmptyStr; |
| 215 |
if FindAtomHeader('data', @AtomSize) then |
| 216 |
begin |
| 217 |
FStream.Seek(8, soCurrent); |
| 218 |
AGenre:= SwapEndian(FStream.ReadWord); |
| 219 |
FStream.Seek(AtomSize - 10, soCurrent); |
| 220 |
end; |
| 221 |
if (AGenre > 0) and (AGenre < MAX_MUSIC_GENRES) then |
| 222 |
begin |
| 223 |
Result:= aTAG_MusicGenre[AGenre - 1]; |
| 224 |
end; |
| 225 |
end; |
| 226 |
|
| 227 |
function TMP4file.ReadTrackData: LongWord; |
| 228 |
var |
| 229 |
AtomSize: Int64; |
| 230 |
begin |
| 231 |
Result:= 0; |
| 232 |
if FindAtomHeader('data', @AtomSize) then |
| 233 |
begin |
| 234 |
FStream.Seek(8, soCurrent); |
| 235 |
Result:= SwapEndian(FStream.ReadDWord); |
| 236 |
FStream.Seek(AtomSize - 12, soCurrent); |
| 237 |
end; |
| 238 |
end; |
| 239 |
|
| 240 |
procedure TMP4file.ReadMetaDataItemList; |
| 241 |
var |
| 242 |
AtomSize: Int64; |
| 243 |
AtomFinish: Int64; |
| 244 |
AtomName: TAtomName; |
| 245 |
begin |
| 246 |
FStream.Seek(0, soBeginning); |
| 247 |
if not FindAtomHeader('moov') then Exit; |
| 248 |
if not FindAtomHeader('udta') then Exit; |
| 249 |
|
| 250 |
if FindAtomHeader('meta') then |
| 251 |
begin |
| 252 |
FStream.Seek(4, soCurrent); |
| 253 |
if FindAtomHeader('ilst', @AtomSize) then |
| 254 |
begin |
| 255 |
AtomFinish := FStream.Position + AtomSize; |
| 256 |
while FStream.Position < AtomFinish do |
| 257 |
begin |
| 258 |
LoadAtomHeader(AtomName, AtomSize); |
| 259 |
if SameText('trkn', AtomName) then |
| 260 |
FTrack:= ReadTrackData |
| 261 |
else if SameText('gnre', AtomName) then |
| 262 |
FGenre:= ReadGenreData |
| 263 |
else if SameText('cprt', AtomName) then |
| 264 |
FCopyright:= ReadAtomData |
| 265 |
else if SameText(#169'art', AtomName) then |
| 266 |
FArtist:= ReadAtomData |
| 267 |
else if SameText(#169'alb', AtomName) then |
| 268 |
FAlbum:= ReadAtomData |
| 269 |
else if SameText(#169'cmt', AtomName) then |
| 270 |
FComment:= ReadAtomData |
| 271 |
else if SameText(#169'day', AtomName) then |
| 272 |
FYear:= ReadAtomData |
| 273 |
else if SameText(#169'nam', AtomName) then |
| 274 |
FTitle:= ReadAtomData |
| 275 |
else if SameText(#169'too', AtomName) then |
| 276 |
FEncoder:= ReadAtomData |
| 277 |
else if SameText(#169'wrt', AtomName) then |
| 278 |
FComposer:= ReadAtomData |
| 279 |
else if SameText(#169'gen', AtomName) then |
| 280 |
FGenre:= ReadAtomData |
| 281 |
else FStream.Seek(AtomSize, soCurrent); |
| 282 |
end; |
| 283 |
end; |
| 284 |
end; |
| 285 |
end; |
| 286 |
|
| 287 |
procedure TMP4file.ReadSampleDescription; |
| 288 |
var |
| 289 |
Number: LongWord; |
| 290 |
begin |
| 291 |
if not FindAtomHeader('moov') then Exit; |
| 292 |
if not FindAtomHeader('trak') then Exit; |
| 293 |
if not FindAtomHeader('mdia') then Exit; |
| 294 |
if not FindAtomHeader('minf') then Exit; |
| 295 |
if not FindAtomHeader('stbl') then Exit; |
| 296 |
|
| 297 |
if FindAtomHeader('stsd') then |
| 298 |
begin |
| 299 |
FStream.Seek(4, soCurrent); |
| 300 |
Number:= SwapEndian(FStream.ReadDWord); |
| 301 |
if Number = 1 then |
| 302 |
begin |
| 303 |
if FindAtomHeader('mp4a') then |
| 304 |
begin |
| 305 |
FStream.Seek(16, soCurrent); |
| 306 |
FChannels:= SwapEndian(FStream.ReadWord); |
| 307 |
FSampleSize:= SwapEndian(FStream.ReadWord); |
| 308 |
FStream.Seek(2, soCurrent); |
| 309 |
FSampleRate:= SwapEndian(FStream.ReadDWord); |
| 310 |
end; |
| 311 |
end; |
| 312 |
end; |
| 313 |
end; |
| 314 |
|
| 315 |
constructor TMP4file.Create; |
| 316 |
begin |
| 317 |
|
| 318 |
end; |
| 319 |
|
| 320 |
destructor TMP4file.Destroy; |
| 321 |
begin |
| 322 |
inherited Destroy; |
| 323 |
end; |
| 324 |
|
| 325 |
function TMP4file.ReadFromFile(const FileName: String): Boolean; |
| 326 |
var |
| 327 |
AtomSize: Int64; |
| 328 |
AtomName: TAtomName; |
| 329 |
begin |
| 330 |
ResetData; |
| 331 |
FStream:= TFileStreamEx.Create(FileName, fmOpenRead or fmShareDenyNone); |
| 332 |
try |
| 333 |
FFileSize:= FStream.Size; |
| 334 |
Result:= LoadAtomHeader(AtomName, AtomSize) and SameText(AtomName, 'ftyp'); |
| 335 |
if Result then |
| 336 |
begin |
| 337 |
FStream.Seek(AtomSize, soCurrent); |
| 338 |
ReadSampleDescription; |
| 339 |
ReadMovieHeader; |
| 340 |
ReadMetaDataItemList; |
| 341 |
end; |
| 342 |
finally |
| 343 |
FreeAndNil(FStream); |
| 344 |
end; |
| 345 |
end; |
| 346 |
|
| 347 |
end. |
| 348 |
|