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
| import androidx.media3.extractor.ExtractorInput; import androidx.media3.extractor.Mp4Extractor; import androidx.media3.extractor.TrackOutput; import androidx.media3.common.util.Util;
import java.io.IOException; import java.io.InputStream;
public class MediaExtractorExample {
public void extractMp4(InputStream inputStream) throws IOException { ExtractorInput extractorInput = new ExtractorInput() { private InputStream input = inputStream;
@Override public long getLength() throws IOException { return input.available(); }
@Override public long getPosition() throws IOException { return 0; }
@Override public void advancePeekPosition(int length) throws IOException { input.skip(length); }
@Override public void resetPeekPosition() throws IOException { }
@Override public int read(byte[] target, int offset, int length) throws IOException { return input.read(target, offset, length); }
@Override public void close() throws IOException { input.close(); } };
Mp4Extractor mp4Extractor = new Mp4Extractor();
TrackOutput trackOutput = new TrackOutput() { @Override public void format(Format format) { System.out.println("Format: " + format); }
@Override public void sampleData(ExtractorInput input, int length) throws IOException { byte[] buffer = new byte[length]; input.readFully(buffer, 0, length); System.out.println("Sample Data: " + new String(buffer)); }
@Override public void sampleMetadata(long timeUs, int flags, int size, int offset, CryptoData cryptoData) { System.out.println("Sample Metadata: timeUs=" + timeUs + ", flags=" + flags); } };
mp4Extractor.init(new ExtractorOutput() { @Override public void seekMap(SeekMap seekMap) { long duration = seekMap.getDurationUs(); System.out.println("Media Duration: " + duration); }
@Override public void track(int id, TrackOutput trackOutput) { System.out.println("Track: " + id); } });
mp4Extractor.read(extractorInput, new PositionHolder(), 0); } }
|