| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package org.jaudiotagger.audio.mp4; |
| 20 | |
|
| 21 | |
import org.jaudiotagger.audio.exceptions.CannotReadException; |
| 22 | |
import org.jaudiotagger.audio.exceptions.CannotReadVideoException; |
| 23 | |
import org.jaudiotagger.audio.generic.GenericAudioHeader; |
| 24 | |
import org.jaudiotagger.audio.mp4.atom.*; |
| 25 | |
import org.jaudiotagger.logging.ErrorMessage; |
| 26 | |
|
| 27 | |
import java.io.IOException; |
| 28 | |
import java.io.RandomAccessFile; |
| 29 | |
import java.nio.ByteBuffer; |
| 30 | |
import java.util.logging.Logger; |
| 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 | 16 | public class Mp4InfoReader |
| 56 | |
{ |
| 57 | |
|
| 58 | 4 | public static Logger logger = Logger.getLogger("org.jaudiotagger.audio.mp4.atom"); |
| 59 | |
|
| 60 | |
public GenericAudioHeader read(RandomAccessFile raf) throws CannotReadException, IOException |
| 61 | |
{ |
| 62 | 406 | Mp4AudioHeader info = new Mp4AudioHeader(); |
| 63 | |
|
| 64 | |
|
| 65 | 406 | Mp4BoxHeader ftypHeader = Mp4BoxHeader.seekWithinLevel(raf, Mp4NotMetaFieldKey.FTYP.getFieldName()); |
| 66 | 406 | if (ftypHeader == null) |
| 67 | |
{ |
| 68 | 0 | throw new CannotReadException(ErrorMessage.MP4_FILE_NOT_CONTAINER.getMsg()); |
| 69 | |
} |
| 70 | 406 | ByteBuffer ftypBuffer = ByteBuffer.allocate(ftypHeader.getLength() - Mp4BoxHeader.HEADER_LENGTH); |
| 71 | 406 | raf.getChannel().read(ftypBuffer); |
| 72 | 406 | ftypBuffer.rewind(); |
| 73 | 406 | Mp4FtypBox ftyp = new Mp4FtypBox(ftypHeader, ftypBuffer); |
| 74 | 406 | ftyp.processData(); |
| 75 | 406 | info.setBrand(ftyp.getMajorBrand()); |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | 406 | Mp4BoxHeader moovHeader = Mp4BoxHeader.seekWithinLevel(raf, Mp4NotMetaFieldKey.MOOV.getFieldName()); |
| 80 | 406 | if (moovHeader == null) |
| 81 | |
{ |
| 82 | 0 | throw new CannotReadException(ErrorMessage.MP4_FILE_NOT_AUDIO.getMsg()); |
| 83 | |
} |
| 84 | 406 | ByteBuffer moovBuffer = ByteBuffer.allocate(moovHeader.getLength() - Mp4BoxHeader.HEADER_LENGTH); |
| 85 | 406 | raf.getChannel().read(moovBuffer); |
| 86 | 406 | moovBuffer.rewind(); |
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | 406 | Mp4BoxHeader boxHeader = Mp4BoxHeader.seekWithinLevel(moovBuffer, Mp4NotMetaFieldKey.MVHD.getFieldName()); |
| 91 | 406 | if (boxHeader == null) |
| 92 | |
{ |
| 93 | 0 | throw new CannotReadException(ErrorMessage.MP4_FILE_NOT_AUDIO.getMsg()); |
| 94 | |
} |
| 95 | 406 | ByteBuffer mvhdBuffer = moovBuffer.slice(); |
| 96 | 406 | Mp4MvhdBox mvhd = new Mp4MvhdBox(boxHeader, mvhdBuffer); |
| 97 | 406 | info.setLength(mvhd.getLength()); |
| 98 | |
|
| 99 | 406 | mvhdBuffer.position(mvhdBuffer.position() + boxHeader.getDataLength()); |
| 100 | |
|
| 101 | |
|
| 102 | 406 | boxHeader = Mp4BoxHeader.seekWithinLevel(mvhdBuffer, Mp4NotMetaFieldKey.TRAK.getFieldName()); |
| 103 | 406 | int endOfFirstTrackInBuffer = mvhdBuffer.position() + boxHeader.getDataLength(); |
| 104 | |
|
| 105 | 406 | if (boxHeader == null) |
| 106 | |
{ |
| 107 | 0 | throw new CannotReadException(ErrorMessage.MP4_FILE_NOT_AUDIO.getMsg()); |
| 108 | |
} |
| 109 | |
|
| 110 | 406 | boxHeader = Mp4BoxHeader.seekWithinLevel(mvhdBuffer, Mp4NotMetaFieldKey.MDIA.getFieldName()); |
| 111 | 406 | if (boxHeader == null) |
| 112 | |
{ |
| 113 | 0 | throw new CannotReadException(ErrorMessage.MP4_FILE_NOT_AUDIO.getMsg()); |
| 114 | |
} |
| 115 | |
|
| 116 | 406 | boxHeader = Mp4BoxHeader.seekWithinLevel(mvhdBuffer, Mp4NotMetaFieldKey.MDHD.getFieldName()); |
| 117 | 406 | if (boxHeader == null) |
| 118 | |
{ |
| 119 | 0 | throw new CannotReadException(ErrorMessage.MP4_FILE_NOT_AUDIO.getMsg()); |
| 120 | |
} |
| 121 | 406 | Mp4MdhdBox mdhd = new Mp4MdhdBox(boxHeader, mvhdBuffer.slice()); |
| 122 | 406 | info.setSamplingRate(mdhd.getSampleRate()); |
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | 406 | mvhdBuffer.position(mvhdBuffer.position() + boxHeader.getDataLength()); |
| 138 | 406 | boxHeader = Mp4BoxHeader.seekWithinLevel(mvhdBuffer, Mp4NotMetaFieldKey.MINF.getFieldName()); |
| 139 | 406 | if (boxHeader == null) |
| 140 | |
{ |
| 141 | 0 | throw new CannotReadException(ErrorMessage.MP4_FILE_NOT_AUDIO.getMsg()); |
| 142 | |
} |
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | 406 | boxHeader = Mp4BoxHeader.seekWithinLevel(mvhdBuffer, Mp4NotMetaFieldKey.SMHD.getFieldName()); |
| 147 | 406 | if (boxHeader == null) |
| 148 | |
{ |
| 149 | 0 | throw new CannotReadException(ErrorMessage.MP4_FILE_NOT_AUDIO.getMsg()); |
| 150 | |
} |
| 151 | 406 | mvhdBuffer.position(mvhdBuffer.position() + boxHeader.getDataLength()); |
| 152 | |
|
| 153 | |
|
| 154 | 406 | boxHeader = Mp4BoxHeader.seekWithinLevel(mvhdBuffer, Mp4NotMetaFieldKey.STBL.getFieldName()); |
| 155 | 406 | if (boxHeader == null) |
| 156 | |
{ |
| 157 | 0 | throw new CannotReadException(ErrorMessage.MP4_FILE_NOT_AUDIO.getMsg()); |
| 158 | |
} |
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | 406 | boxHeader = Mp4BoxHeader.seekWithinLevel(mvhdBuffer, Mp4NotMetaFieldKey.STSD.getFieldName()); |
| 163 | 406 | if (boxHeader != null) |
| 164 | |
{ |
| 165 | 406 | Mp4StsdBox stsd = new Mp4StsdBox(boxHeader, mvhdBuffer); |
| 166 | 406 | stsd.processData(); |
| 167 | 406 | int positionAfterStsdHeaderAndData = mvhdBuffer.position(); |
| 168 | |
|
| 169 | |
|
| 170 | 406 | boxHeader = Mp4BoxHeader.seekWithinLevel(mvhdBuffer, Mp4NotMetaFieldKey.MP4A.getFieldName()); |
| 171 | 406 | if (boxHeader != null) |
| 172 | |
{ |
| 173 | 372 | ByteBuffer mp4aBuffer = mvhdBuffer.slice(); |
| 174 | 372 | Mp4Mp4aBox mp4a = new Mp4Mp4aBox(boxHeader, mp4aBuffer); |
| 175 | 372 | mp4a.processData(); |
| 176 | |
|
| 177 | 372 | boxHeader = Mp4BoxHeader.seekWithinLevel(mp4aBuffer, Mp4NotMetaFieldKey.ESDS.getFieldName()); |
| 178 | 372 | if (boxHeader != null) |
| 179 | |
{ |
| 180 | 372 | Mp4EsdsBox esds = new Mp4EsdsBox(boxHeader, mp4aBuffer.slice()); |
| 181 | |
|
| 182 | |
|
| 183 | 372 | info.setBitrate(esds.getAvgBitrate() / 1000); |
| 184 | |
|
| 185 | |
|
| 186 | 372 | info.setChannelNumber(esds.getNumberOfChannels()); |
| 187 | |
|
| 188 | 372 | info.setKind(esds.getKind()); |
| 189 | 372 | info.setProfile(esds.getAudioProfile()); |
| 190 | |
|
| 191 | 372 | info.setEncodingType(EncoderType.AAC.getDescription()); |
| 192 | |
} |
| 193 | 372 | } |
| 194 | |
else |
| 195 | |
{ |
| 196 | |
|
| 197 | 34 | mvhdBuffer.position(positionAfterStsdHeaderAndData); |
| 198 | 34 | boxHeader = Mp4BoxHeader.seekWithinLevel(mvhdBuffer, Mp4NotMetaFieldKey.DRMS.getFieldName()); |
| 199 | 34 | if (boxHeader != null) |
| 200 | |
{ |
| 201 | 13 | Mp4DrmsBox drms = new Mp4DrmsBox(boxHeader, mvhdBuffer); |
| 202 | 13 | drms.processData(); |
| 203 | |
|
| 204 | |
|
| 205 | 13 | boxHeader = Mp4BoxHeader.seekWithinLevel(mvhdBuffer, Mp4NotMetaFieldKey.ESDS.getFieldName()); |
| 206 | 13 | if (boxHeader != null) |
| 207 | |
{ |
| 208 | 13 | Mp4EsdsBox esds = new Mp4EsdsBox(boxHeader, mvhdBuffer.slice()); |
| 209 | |
|
| 210 | |
|
| 211 | 13 | info.setBitrate(esds.getAvgBitrate() / 1000); |
| 212 | |
|
| 213 | |
|
| 214 | 13 | info.setChannelNumber(esds.getNumberOfChannels()); |
| 215 | |
|
| 216 | 13 | info.setKind(esds.getKind()); |
| 217 | 13 | info.setProfile(esds.getAudioProfile()); |
| 218 | |
|
| 219 | 13 | info.setEncodingType(EncoderType.DRM_AAC.getDescription()); |
| 220 | |
} |
| 221 | 13 | } |
| 222 | |
|
| 223 | |
else |
| 224 | |
{ |
| 225 | 21 | mvhdBuffer.position(positionAfterStsdHeaderAndData); |
| 226 | 21 | boxHeader = Mp4BoxHeader.seekWithinLevel(mvhdBuffer, Mp4NotMetaFieldKey.ALAC.getFieldName()); |
| 227 | 21 | if (boxHeader != null) |
| 228 | |
{ |
| 229 | |
|
| 230 | 21 | Mp4AlacBox alac = new Mp4AlacBox(boxHeader, mvhdBuffer); |
| 231 | 21 | alac.processData(); |
| 232 | |
|
| 233 | |
|
| 234 | 21 | boxHeader = Mp4BoxHeader.seekWithinLevel(mvhdBuffer, Mp4NotMetaFieldKey.ALAC.getFieldName()); |
| 235 | 21 | if (boxHeader != null) |
| 236 | |
{ |
| 237 | 21 | alac = new Mp4AlacBox(boxHeader, mvhdBuffer); |
| 238 | 21 | alac.processData(); |
| 239 | 21 | info.setEncodingType(EncoderType.APPLE_LOSSLESS.getDescription()); |
| 240 | 21 | info.setChannelNumber(alac.getChannels()); |
| 241 | 21 | info.setBitrate(alac.getBitRate()/1000); |
| 242 | |
} |
| 243 | |
} |
| 244 | |
} |
| 245 | |
} |
| 246 | |
} |
| 247 | |
|
| 248 | 406 | if (info.getChannelNumber() == -1) |
| 249 | |
{ |
| 250 | 0 | info.setChannelNumber(2); |
| 251 | |
} |
| 252 | |
|
| 253 | |
|
| 254 | 406 | if (info.getBitRateAsNumber() == -1) |
| 255 | |
{ |
| 256 | 0 | info.setBitrate(128); |
| 257 | |
} |
| 258 | |
|
| 259 | |
|
| 260 | 406 | if (info.getEncodingType().equals("")) |
| 261 | |
{ |
| 262 | 0 | info.setEncodingType(EncoderType.AAC.getDescription()); |
| 263 | |
} |
| 264 | |
|
| 265 | 406 | logger.info(info.toString()); |
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | 406 | mvhdBuffer.position(endOfFirstTrackInBuffer); |
| 270 | 406 | boxHeader = Mp4BoxHeader.seekWithinLevel(mvhdBuffer, Mp4NotMetaFieldKey.TRAK.getFieldName()); |
| 271 | 405 | if (boxHeader != null) |
| 272 | |
{ |
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | 54 | if (ftyp.getMajorBrand().equals(Mp4FtypBox.Brand.ISO14496_1_VERSION_2.getId()) |
| 277 | |
|| ftyp.getMajorBrand().equals(Mp4FtypBox.Brand.APPLE_AUDIO_ONLY.getId()) |
| 278 | |
|| ftyp.getMajorBrand().equals(Mp4FtypBox.Brand.APPLE_AUDIO.getId())) |
| 279 | |
{ |
| 280 | |
|
| 281 | |
|
| 282 | 53 | boxHeader = Mp4BoxHeader.seekWithinLevel(mvhdBuffer, Mp4NotMetaFieldKey.MDIA.getFieldName()); |
| 283 | 53 | if (boxHeader == null) |
| 284 | |
{ |
| 285 | 0 | throw new CannotReadVideoException(ErrorMessage.MP4_FILE_IS_VIDEO.getMsg()); |
| 286 | |
} |
| 287 | |
|
| 288 | 53 | boxHeader = Mp4BoxHeader.seekWithinLevel(mvhdBuffer, Mp4NotMetaFieldKey.MDHD.getFieldName()); |
| 289 | 53 | if (boxHeader == null) |
| 290 | |
{ |
| 291 | 0 | throw new CannotReadVideoException(ErrorMessage.MP4_FILE_IS_VIDEO.getMsg()); |
| 292 | |
} |
| 293 | |
|
| 294 | 53 | mvhdBuffer.position(mvhdBuffer.position() + boxHeader.getDataLength()); |
| 295 | 53 | boxHeader = Mp4BoxHeader.seekWithinLevel(mvhdBuffer, Mp4NotMetaFieldKey.MINF.getFieldName()); |
| 296 | 53 | if (boxHeader == null) |
| 297 | |
{ |
| 298 | 0 | throw new CannotReadVideoException(ErrorMessage.MP4_FILE_IS_VIDEO.getMsg()); |
| 299 | |
} |
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | 53 | boxHeader = Mp4BoxHeader.seekWithinLevel(mvhdBuffer, Mp4NotMetaFieldKey.NMHD.getFieldName()); |
| 304 | 53 | if (boxHeader == null) |
| 305 | |
{ |
| 306 | 0 | throw new CannotReadVideoException(ErrorMessage.MP4_FILE_IS_VIDEO.getMsg()); |
| 307 | |
} |
| 308 | |
} |
| 309 | |
else |
| 310 | |
{ |
| 311 | 1 | logger.info(ErrorMessage.MP4_FILE_IS_VIDEO.getMsg() + ":" + ftyp.getMajorBrand()); |
| 312 | 1 | throw new CannotReadVideoException(ErrorMessage.MP4_FILE_IS_VIDEO.getMsg()); |
| 313 | |
} |
| 314 | |
} |
| 315 | |
|
| 316 | |
|
| 317 | 404 | Mp4AtomTree atomTree = new Mp4AtomTree(raf,false); |
| 318 | |
|
| 319 | 401 | return info; |
| 320 | |
} |
| 321 | |
|
| 322 | |
|
| 323 | |
} |