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) { handleFailure(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) { handleFailure(e); } finally { // 3. Release the extractor extractor.release(); } }