MediaExtractorCompat क्लास, प्लैटफ़ॉर्म की MediaExtractor क्लास की जगह इस्तेमाल की जा सकती है. साथ ही, यह एक जैसे एपीआई और फ़ंक्शन उपलब्ध कराती है. इससे डेटा सोर्स से, डीमक्स किए गए मीडिया डेटा को एक्सट्रैक्ट करने में मदद मिलती है. आम तौर पर, यह डेटा एन्कोड किया गया होता है.
यह कंटेनर फ़ाइल (जैसे कि MP4 या MKV) को उसके अलग-अलग ट्रैक में बांटता है. जैसे, वीडियो, ऑडियो, और सबटाइटल. इसके बाद, एक्सट्रैक्टर इन ट्रैक से मिले रॉ, एन्कोड किए गए डेटा को सैंपल के क्रम के तौर पर पढ़ता है. जैसे, कंप्रेस किया गया कोई वीडियो फ़्रेम या ऑडियो का ब्लॉक. इसके बाद, इन्हें डिकोडर को भेजा जाता है.
इसके सामान्य इस्तेमाल के उदाहरणों में ये शामिल हैं:
- ट्रांसकोडिंग या रीमक्सिंग: किसी ट्रैक से एन्कोड किए गए सैंपल को पढ़ना, ताकि कोडेक (ट्रांसकोडिंग) बदला जा सके या स्ट्रीम को नए कंटेनर (रीमक्सिंग) में फिर से पैक किया जा सके. जैसे, किसी MP4 फ़ाइल को MKV में बदलना.
- चुनिंदा कॉन्टेंट निकालना: किसी एक ट्रैक को अलग करना और सेव करना. जैसे, किसी वीडियो फ़ाइल से ऑडियो स्ट्रीम निकालना.
- लो-लेवल डीबगिंग: फ़ाइल में मौजूद गड़बड़ी, टाइमस्टैंप से जुड़ी समस्याएं या अन्य समस्याओं को ठीक करने के लिए, अलग-अलग सैंपल की जांच करना.
- कस्टम प्लेयर बनाना: खास इस्तेमाल के मामलों के लिए, मीडिया पाइपलाइन पर पूरा कंट्रोल रखने वाला कस्टम प्लेयर बनाना.
खास जानकारी
यहां दिए गए कोड सैंपल में, MediaExtractorCompat का इस्तेमाल करने का तरीका बताया गया है:
Kotlin
fun extractSamples(context: Context, mediaPath: String) {
val extractor = MediaExtractorCompat(context)
try {
// 1. Setup the extractor
extractor.setDataSource(mediaPath)
// Find and select available tracks
for (i in 0 until extractor.trackCount) {
val format = extractor.getTrackFormat(i)
extractor.selectTrack(i)
}
// 2. Process samples
val buffer = ByteBuffer.allocate(10 * 1024 * 1024)
while (true) {
// Read an encoded sample into the buffer.
val bytesRead = extractor.readSampleData(buffer, 0)
if (bytesRead < 0) break
// Access sample metadata
val trackIndex = extractor.sampleTrackIndex
val presentationTimeUs = extractor.sampleTime
val sampleSize = extractor.sampleSize
extractor.advance()
}
} catch (e: IOException) {
throw RuntimeException(e)
} finally {
// 3. Release the extractor
extractor.release()
}
}
Java
public void extractSamples(Context context, String mediaPath) {
MediaExtractorCompat extractor = new MediaExtractorCompat(context);
try {
// 1. Setup the extractor
extractor.setDataSource(mediaPath);
// Find and select available tracks
for (int i = 0; i < extractor.getTrackCount(); i++) {
MediaFormat format = extractor.getTrackFormat(i);
extractor.selectTrack(i);
}
// 2. Process samples
ByteBuffer buffer = ByteBuffer.allocate(10 * 1024 * 1024);
while (true) {
// Read an encoded sample into the buffer.
int bytesRead = extractor.readSampleData(buffer, 0);
if (bytesRead < 0) break;
// Access sample metadata
int trackIndex = extractor.getSampleTrackIndex();
long presentationTimeUs = extractor.getSampleTime();
long sampleSize = extractor.getSampleSize();
extractor.advance();
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
// 3. Release the extractor
extractor.release();
}
}