| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
package com.drew.metadata.avi; |
| 22 |
|
| 23 |
import com.drew.imaging.riff.RiffHandler; |
| 24 |
import com.drew.lang.ByteArrayReader; |
| 25 |
import com.drew.lang.annotations.NotNull; |
| 26 |
import com.drew.metadata.Metadata; |
| 27 |
|
| 28 |
import java.io.IOException; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public class AviRiffHandler implements RiffHandler |
| 47 |
{ |
| 48 |
@NotNull |
| 49 |
private final AviDirectory _directory; |
| 50 |
|
| 51 |
public AviRiffHandler(@NotNull Metadata metadata) |
| 52 |
{ |
| 53 |
_directory = new AviDirectory(); |
| 54 |
metadata.addDirectory(_directory); |
| 55 |
} |
| 56 |
|
| 57 |
public boolean shouldAcceptRiffIdentifier(@NotNull String identifier) |
| 58 |
{ |
| 59 |
return identifier.equals(AviDirectory.FORMAT); |
| 60 |
} |
| 61 |
|
| 62 |
public boolean shouldAcceptChunk(@NotNull String fourCC) |
| 63 |
{ |
| 64 |
return fourCC.equals(AviDirectory.CHUNK_STREAM_HEADER) |
| 65 |
|| fourCC.equals(AviDirectory.CHUNK_MAIN_HEADER) |
| 66 |
|| fourCC.equals(AviDirectory.CHUNK_DATETIME_ORIGINAL); |
| 67 |
} |
| 68 |
|
| 69 |
public boolean shouldAcceptList(@NotNull String fourCC) |
| 70 |
{ |
| 71 |
return fourCC.equals(AviDirectory.LIST_HEADER) |
| 72 |
|| fourCC.equals(AviDirectory.LIST_STREAM_HEADER) |
| 73 |
|| fourCC.equals(AviDirectory.FORMAT); |
| 74 |
} |
| 75 |
|
| 76 |
public void processChunk(@NotNull String fourCC, @NotNull byte[] payload) |
| 77 |
{ |
| 78 |
try { |
| 79 |
if (fourCC.equals(AviDirectory.CHUNK_STREAM_HEADER)) { |
| 80 |
ByteArrayReader reader = new ByteArrayReader(payload); |
| 81 |
reader.setMotorolaByteOrder(false); |
| 82 |
|
| 83 |
String fccType = new String(reader.getBytes(0, 4)); |
| 84 |
String fccHandler = new String(reader.getBytes(4, 4)); |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
float dwScale = reader.getFloat32(20); |
| 90 |
float dwRate = reader.getFloat32(24); |
| 91 |
|
| 92 |
int dwLength = reader.getInt32(32); |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
if (fccType.equals("vids")) { |
| 99 |
if (!_directory.containsTag(AviDirectory.TAG_FRAMES_PER_SECOND)) { |
| 100 |
_directory.setDouble(AviDirectory.TAG_FRAMES_PER_SECOND, (dwRate / dwScale)); |
| 101 |
|
| 102 |
double duration = dwLength / (dwRate / dwScale); |
| 103 |
int hours = (int) duration / (int) (Math.pow(60, 2)); |
| 104 |
int minutes = ((int) duration / (int) (Math.pow(60, 1))) - (hours * 60); |
| 105 |
int seconds = (int) Math.round((duration / (Math.pow(60, 0))) - (minutes * 60)); |
| 106 |
String time = String.format("%1$02d:%2$02d:%3$02d", hours, minutes, seconds); |
| 107 |
|
| 108 |
_directory.setString(AviDirectory.TAG_DURATION, time); |
| 109 |
_directory.setString(AviDirectory.TAG_VIDEO_CODEC, fccHandler); |
| 110 |
} |
| 111 |
} else if (fccType.equals("auds")) { |
| 112 |
if (!_directory.containsTag(AviDirectory.TAG_SAMPLES_PER_SECOND)) { |
| 113 |
_directory.setDouble(AviDirectory.TAG_SAMPLES_PER_SECOND, (dwRate / dwScale)); |
| 114 |
} |
| 115 |
} |
| 116 |
} else if (fourCC.equals(AviDirectory.CHUNK_MAIN_HEADER)) { |
| 117 |
ByteArrayReader reader = new ByteArrayReader(payload); |
| 118 |
reader.setMotorolaByteOrder(false); |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
int dwStreams = reader.getInt32(24); |
| 127 |
|
| 128 |
int dwWidth = reader.getInt32(32); |
| 129 |
int dwHeight = reader.getInt32(36); |
| 130 |
|
| 131 |
|
| 132 |
_directory.setInt(AviDirectory.TAG_WIDTH, dwWidth); |
| 133 |
_directory.setInt(AviDirectory.TAG_HEIGHT, dwHeight); |
| 134 |
_directory.setInt(AviDirectory.TAG_STREAMS, dwStreams); |
| 135 |
} else if (fourCC.equals(AviDirectory.CHUNK_DATETIME_ORIGINAL)) { |
| 136 |
ByteArrayReader reader = new ByteArrayReader(payload); |
| 137 |
String str = reader.getString(0, payload.length, "ASCII"); |
| 138 |
if (str.length() == 26 && str.endsWith(String.valueOf(new char[] { 0x0A, 0x00 }))) { |
| 139 |
|
| 140 |
str = str.substring(0, 24); |
| 141 |
} |
| 142 |
_directory.setString(AviDirectory.TAG_DATETIME_ORIGINAL, str); |
| 143 |
} |
| 144 |
} catch (IOException ex) { |
| 145 |
_directory.addError(ex.getMessage()); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
public void addError(@NotNull String message) |
| 151 |
{ |
| 152 |
_directory.addError(message); |
| 153 |
} |
| 154 |
} |