コード例 #1
0
ファイル: mediaparser.js プロジェクト: Eke/jwplayer
const mediaparser = function (obj, item) {
    // Prefix for the MRSS namespace
    const PREFIX = 'media';
    const tracks = 'tracks';
    const captions = [];

    function getLabel(code) {
        const LANGS = {
            zh: 'Chinese',
            nl: 'Dutch',
            en: 'English',
            fr: 'French',
            de: 'German',
            it: 'Italian',
            ja: 'Japanese',
            pt: 'Portuguese',
            ru: 'Russian',
            es: 'Spanish'
        };
        if (LANGS[code]) {
            return LANGS[code];
        }
        return code;
    }

    for (let i = 0; i < numChildren(obj); i++) {
        const node = obj.childNodes[i];
        if (node.prefix === PREFIX) {
            if (!localName(node)) {
                continue;
            }
            switch (localName(node).toLowerCase()) {
                case 'content':
                    if (xmlAttribute(node, 'duration')) {
                        item.duration = utils.seconds(xmlAttribute(node, 'duration'));
                    }
                    if (xmlAttribute(node, 'url')) {
                        if (!item.sources) {
                            item.sources = [];
                        }
                        var sources = {
                            file: xmlAttribute(node, 'url'),
                            type: xmlAttribute(node, 'type'),
                            width: xmlAttribute(node, 'width'),
                            label: xmlAttribute(node, 'label')
                        };

                        var mediaTypes = findMediaTypes(node);
                        if (mediaTypes.length) {
                            sources.mediaTypes = mediaTypes;
                        }

                        item.sources.push(sources);
                    }
                    if (numChildren(node) > 0) {
                        item = mediaparser(node, item);
                    }
                    break;
                case 'title':
                    item.title = textContent(node);
                    break;
                case 'description':
                    item.description = textContent(node);
                    break;
                case 'guid':
                    item.mediaid = textContent(node);
                    break;
                case 'thumbnail':
                    if (!item.image) {
                        item.image = xmlAttribute(node, 'url');
                    }
                    break;
                case 'group':
                    mediaparser(node, item);
                    break;
                case 'subtitle':
                    var entry = {};
                    entry.file = xmlAttribute(node, 'url');
                    entry.kind = 'captions';
                    if (xmlAttribute(node, 'lang').length > 0) {
                        entry.label = getLabel(xmlAttribute(node, 'lang'));
                    }
                    captions.push(entry);
                    break;
                default:
                    break;
            }
        }
    }

    if (!item.hasOwnProperty(tracks)) {
        item[tracks] = [];
    }

    for (let i = 0; i < captions.length; i++) {
        item[tracks].push(captions[i]);
    }
    return item;
};
コード例 #2
0
ファイル: jwparser.js プロジェクト: jwplayer/jwplayer
const parseEntry = function (obj, itm) {
    const PREFIX = 'jwplayer';
    const def = 'default';
    const label = 'label';
    const file = 'file';
    const type = 'type';
    const sources = [];
    const tracks = [];

    for (let i = 0; i < obj.childNodes.length; i++) {
        const node = obj.childNodes[i];
        if (node.prefix === PREFIX) {
            const _localName = localName(node);
            if (_localName === 'source') {
                delete itm.sources;
                sources.push({
                    file: xmlAttribute(node, file),
                    'default': xmlAttribute(node, def),
                    label: xmlAttribute(node, label),
                    type: xmlAttribute(node, type)
                });
            } else if (_localName === 'track') {
                delete itm.tracks;
                tracks.push({
                    file: xmlAttribute(node, file),
                    'default': xmlAttribute(node, def),
                    kind: xmlAttribute(node, 'kind'),
                    label: xmlAttribute(node, label)
                });
            } else {
                itm[_localName] = serialize(textContent(node));
                if (_localName === 'file' && itm.sources) {
                    // jwplayer namespace file should override existing source
                    // (probably set in MediaParser)
                    delete itm.sources;
                }
            }

        }
        if (!itm[file]) {
            itm[file] = itm.link;
        }
    }


    if (sources.length) {
        itm.sources = [];
        for (let i = 0; i < sources.length; i++) {
            if (sources[i].file.length > 0) {
                sources[i][def] = (sources[i][def] === 'true');
                if (!sources[i].label.length) {
                    delete sources[i].label;
                }
                itm.sources.push(sources[i]);
            }
        }
    }

    if (tracks.length) {
        itm.tracks = [];
        for (let i = 0; i < tracks.length; i++) {
            if (tracks[i].file.length > 0) {
                tracks[i][def] = (tracks[i][def] === 'true') ? true : false;
                tracks[i].kind = (!tracks[i].kind.length) ? 'captions' : tracks[i].kind;
                if (!tracks[i].label.length) {
                    delete tracks[i].label;
                }
                itm.tracks.push(tracks[i]);
            }
        }
    }
    return itm;
};