Python & YoutubeDL, retrive info-json, modify it and work with it

Question:

I need to work with only a part of the info-json that youtubedl obtains: the problem is in a video platform where, in the browser, you can watch the video or download/hear the audio file (that is besides the internal audio files from the video, like the "visual" option and the "podcast" option).
The problem is that it downloads both files (video+audio), this is how I obtain the info:

ydl = youtube_dl.YoutubeDL({'retries': 10 })
result = ydl.extract_info(vurl, download=False)

Where vurl is the video url; the json that I get in result is:

{
    "_type": "playlist",
    "entries": [
        {
            "formats": [
                {
                    "format_id": "3",
                    "manifest_url": "https://video.brighteon.com/file/BTBucket-Prod/dash/6e8ecf12-f7f8-4565-a8ae-a52c76333556.mpd",
                    "ext": "m4a",
                    "width": null,
                    "height": null,
                    "tbr": 96.0,
                    "asr": 48000,
                    "fps": null,
                    "language": "eng",
                    "format_note": "DASH audio",
                    "filesize": null,
                    "container": "m4a_dash",
                    "vcodec": "none",
                    "acodec": "mp4a.40.5",
                    "url": "https://video.brighteon.com/file/BTBucket-Prod/dash/6e8ecf12-f7f8-4565-a8ae-a52c76333556_audio.mp4",
                    "http_headers": {
                        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3680.1 Safari/537.36",
                        "Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
                        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
                        "Accept-Encoding": "gzip, deflate",
                        "Accept-Language": "en-us,en;q=0.5",
                        "Referer": "https://www.brighteon.com/cadd23ad-e855-4c1b-ba6d-a605c71a07c9"
                    },
                    "format": "3 - audio only (DASH audio)",
                    "protocol": "https"
                },
                {
                    "format_id": "2",
                    "manifest_url": "https://video.brighteon.com/file/BTBucket-Prod/dash/6e8ecf12-f7f8-4565-a8ae-a52c76333556.mpd",
                    "ext": "mp4",
                    "width": 480,
                    "height": 270,
                    "tbr": 350.0,
                    "asr": null,
                    "fps": null,
                    "language": null,
                    "format_note": "DASH video",
                    "filesize": null,
                    "container": "mp4_dash",
                    "vcodec": "avc1.4d401f",
                    "acodec": "none",
                    "url": "https://video.brighteon.com/file/BTBucket-Prod/dash/6e8ecf12-f7f8-4565-a8ae-a52c76333556_270.mp4",
                    "http_headers": {
                        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3680.1 Safari/537.36",
                        "Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
                        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
                        "Accept-Encoding": "gzip, deflate",
                        "Accept-Language": "en-us,en;q=0.5",
                        "Referer": "https://www.brighteon.com/cadd23ad-e855-4c1b-ba6d-a605c71a07c9"
                    },
                    "format": "2 - 480x270 (DASH video)",
                    "protocol": "https"
                },
                {
                    "format_id": "1",
                    "manifest_url": "https://video.brighteon.com/file/BTBucket-Prod/dash/6e8ecf12-f7f8-4565-a8ae-a52c76333556.mpd",
                    "ext": "mp4",
                    "width": 960,
                    "height": 540,
                    "tbr": 1500.0,
                    "asr": null,
                    "fps": null,
                    "language": null,
                    "format_note": "DASH video",
                    "filesize": null,
                    "container": "mp4_dash",
                    "vcodec": "avc1.640028",
                    "acodec": "none",
                    "url": "https://video.brighteon.com/file/BTBucket-Prod/dash/6e8ecf12-f7f8-4565-a8ae-a52c76333556_540.mp4",
                    "http_headers": {
                        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3680.1 Safari/537.36",
                        "Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
                        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
                        "Accept-Encoding": "gzip, deflate",
                        "Accept-Language": "en-us,en;q=0.5",
                        "Referer": "https://www.brighteon.com/cadd23ad-e855-4c1b-ba6d-a605c71a07c9"
                    },
                    "format": "1 - 960x540 (DASH video)",
                    "protocol": "https"
                },
                {
                    "format_id": "hls-219",
                    "url": "https://video.brighteon.com/file/BTBucket-Prod/hls/6e8ecf12-f7f8-4565-a8ae-a52c76333556_270.m3u8",
                    "manifest_url": "https://video.brighteon.com/file/BTBucket-Prod/hls/6e8ecf12-f7f8-4565-a8ae-a52c76333556.m3u8",
                    "tbr": 219.293,
                    "ext": "mp4",
                    "fps": 14.985,
                    "protocol": "m3u8",
                    "preference": null,
                    "width": 480,
                    "height": 270,
                    "vcodec": "avc1.4d401f",
                    "acodec": "mp4a.40.5",
                    "http_headers": {
                        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3680.1 Safari/537.36",
                        "Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
                        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
                        "Accept-Encoding": "gzip, deflate",
                        "Accept-Language": "en-us,en;q=0.5",
                        "Referer": "https://www.brighteon.com/cadd23ad-e855-4c1b-ba6d-a605c71a07c9"
                    },
                    "format": "hls-219 - 480x270"
                },
                {
                    "format_id": "hls-475",
                    "url": "https://video.brighteon.com/file/BTBucket-Prod/hls/6e8ecf12-f7f8-4565-a8ae-a52c76333556_540.m3u8",
                    "manifest_url": "https://video.brighteon.com/file/BTBucket-Prod/hls/6e8ecf12-f7f8-4565-a8ae-a52c76333556.m3u8",
                    "tbr": 475.81,
                    "ext": "mp4",
                    "fps": 29.97,
                    "protocol": "m3u8",
                    "preference": null,
                    "width": 960,
                    "height": 540,
                    "vcodec": "avc1.4d401f",
                    "acodec": "mp4a.40.5",
                    "http_headers": {
                        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3680.1 Safari/537.36",
                        "Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
                        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
                        "Accept-Encoding": "gzip, deflate",
                        "Accept-Language": "en-us,en;q=0.5",
                        "Referer": "https://www.brighteon.com/cadd23ad-e855-4c1b-ba6d-a605c71a07c9"
                    },
                    "format": "hls-475 - 960x540"
                }
            ],
            "subtitles": {},
            "thumbnail": "https://photos.brighteon.com/file/brighteon-thumbnails/poster/05fc220c-5fa5-4ac5-b2e4-96229bce92cf",
            "id": "cadd23ad-e855-4c1b-ba6d-a605c71a07c9-1",
            "title": "Situation Update, 10/24/22 - Dirty bomb false flag event to IGNITE World War with Russia (1)",
            "n_entries": 2,
            "playlist": "Situation Update, 10/24/22 - Dirty bomb false flag event to IGNITE World War with Russia",
            "playlist_id": "cadd23ad-e855-4c1b-ba6d-a605c71a07c9",
            "playlist_title": "Situation Update, 10/24/22 - Dirty bomb false flag event to IGNITE World War with Russia",
            "playlist_uploader": null,
            "playlist_uploader_id": null,
            "playlist_index": 1,
            "extractor": "generic",
            "webpage_url": "https://www.brighteon.com/cadd23ad-e855-4c1b-ba6d-a605c71a07c9",
            "webpage_url_basename": "cadd23ad-e855-4c1b-ba6d-a605c71a07c9",
            "extractor_key": "Generic",
            "thumbnails": [
                {
                    "url": "https://photos.brighteon.com/file/brighteon-thumbnails/poster/05fc220c-5fa5-4ac5-b2e4-96229bce92cf",
                    "id": "0"
                }
            ],
            "display_id": "cadd23ad-e855-4c1b-ba6d-a605c71a07c9-1",
            "requested_subtitles": null,
            "requested_formats": [
                {
                    "format_id": "1",
                    "manifest_url": "https://video.brighteon.com/file/BTBucket-Prod/dash/6e8ecf12-f7f8-4565-a8ae-a52c76333556.mpd",
                    "ext": "mp4",
                    "width": 960,
                    "height": 540,
                    "tbr": 1500.0,
                    "asr": null,
                    "fps": null,
                    "language": null,
                    "format_note": "DASH video",
                    "filesize": null,
                    "container": "mp4_dash",
                    "vcodec": "avc1.640028",
                    "acodec": "none",
                    "url": "https://video.brighteon.com/file/BTBucket-Prod/dash/6e8ecf12-f7f8-4565-a8ae-a52c76333556_540.mp4",
                    "http_headers": {
                        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3680.1 Safari/537.36",
                        "Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
                        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
                        "Accept-Encoding": "gzip, deflate",
                        "Accept-Language": "en-us,en;q=0.5",
                        "Referer": "https://www.brighteon.com/cadd23ad-e855-4c1b-ba6d-a605c71a07c9"
                    },
                    "format": "1 - 960x540 (DASH video)",
                    "protocol": "https"
                },
                {
                    "format_id": "3",
                    "manifest_url": "https://video.brighteon.com/file/BTBucket-Prod/dash/6e8ecf12-f7f8-4565-a8ae-a52c76333556.mpd",
                    "ext": "m4a",
                    "width": null,
                    "height": null,
                    "tbr": 96.0,
                    "asr": 48000,
                    "fps": null,
                    "language": "eng",
                    "format_note": "DASH audio",
                    "filesize": null,
                    "container": "m4a_dash",
                    "vcodec": "none",
                    "acodec": "mp4a.40.5",
                    "url": "https://video.brighteon.com/file/BTBucket-Prod/dash/6e8ecf12-f7f8-4565-a8ae-a52c76333556_audio.mp4",
                    "http_headers": {
                        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3680.1 Safari/537.36",
                        "Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
                        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
                        "Accept-Encoding": "gzip, deflate",
                        "Accept-Language": "en-us,en;q=0.5",
                        "Referer": "https://www.brighteon.com/cadd23ad-e855-4c1b-ba6d-a605c71a07c9"
                    },
                    "format": "3 - audio only (DASH audio)",
                    "protocol": "https"
                }
            ],
            "format": "1 - 960x540 (DASH video)+3 - audio only (DASH audio)",
            "format_id": "1+3",
            "width": 960,
            "height": 540,
            "resolution": null,
            "fps": null,
            "vcodec": "avc1.640028",
            "vbr": null,
            "stretched_ratio": null,
            "acodec": "mp4a.40.5",
            "abr": null,
            "ext": "mp4"
        },
        {
            "formats": [
                {
                    "ext": "mp3",
                    "width": null,
                    "height": null,
                    "tbr": null,
                    "format_id": "0",
                    "url": "https://video.brighteon.com/file/BTBucket-Prod/audio/6e8ecf12-f7f8-4565-a8ae-a52c76333556.mp3",
                    "vcodec": "none",
                    "http_headers": {
                        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3680.1 Safari/537.36",
                        "Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
                        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
                        "Accept-Encoding": "gzip, deflate",
                        "Accept-Language": "en-us,en;q=0.5",
                        "Referer": "https://www.brighteon.com/cadd23ad-e855-4c1b-ba6d-a605c71a07c9"
                    },
                    "format": "0 - audio only",
                    "protocol": "https"
                }
            ],
            "subtitles": {},
            "thumbnail": null,
            "id": "cadd23ad-e855-4c1b-ba6d-a605c71a07c9-2",
            "title": "Situation Update, 10/24/22 - Dirty bomb false flag event to IGNITE World War with Russia (2)",
            "n_entries": 2,
            "playlist": "Situation Update, 10/24/22 - Dirty bomb false flag event to IGNITE World War with Russia",
            "playlist_id": "cadd23ad-e855-4c1b-ba6d-a605c71a07c9",
            "playlist_title": "Situation Update, 10/24/22 - Dirty bomb false flag event to IGNITE World War with Russia",
            "playlist_uploader": null,
            "playlist_uploader_id": null,
            "playlist_index": 2,
            "extractor": "generic",
            "webpage_url": "https://www.brighteon.com/cadd23ad-e855-4c1b-ba6d-a605c71a07c9",
            "webpage_url_basename": "cadd23ad-e855-4c1b-ba6d-a605c71a07c9",
            "extractor_key": "Generic",
            "display_id": "cadd23ad-e855-4c1b-ba6d-a605c71a07c9-2",
            "requested_subtitles": null,
            "ext": "mp3",
            "width": null,
            "height": null,
            "tbr": null,
            "format_id": "0",
            "url": "https://video.brighteon.com/file/BTBucket-Prod/audio/6e8ecf12-f7f8-4565-a8ae-a52c76333556.mp3",
            "vcodec": "none",
            "http_headers": {
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3680.1 Safari/537.36",
                "Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
                "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
                "Accept-Encoding": "gzip, deflate",
                "Accept-Language": "en-us,en;q=0.5",
                "Referer": "https://www.brighteon.com/cadd23ad-e855-4c1b-ba6d-a605c71a07c9"
            },
            "format": "0 - audio only",
            "protocol": "https"
        }
    ],
    "id": "cadd23ad-e855-4c1b-ba6d-a605c71a07c9",
    "title": "Situation Update, 10/24/22 - Dirty bomb false flag event to IGNITE World War with Russia",
    "extractor": "generic",
    "webpage_url": "https://www.brighteon.com/cadd23ad-e855-4c1b-ba6d-a605c71a07c9",
    "webpage_url_basename": "cadd23ad-e855-4c1b-ba6d-a605c71a07c9",
    "extractor_key": "Generic"
}

I want to modify the json (I need to delete the elements from json["entries"] that in element["ext"] do not have a video extension) and then use the modified json to download the video; is it possible?, and, in case it is, how?
Thanks in advance,

Asked By: Gerardo

||

Answers:

If you want to delete the deepest list (like: result["entries"][0]["formats"][0])

for i in result['entries']:
    index=0
    for j in i['formats']:
        if j['ext'] != 'mp4':
            print("Info --- > Deleting: ",i['formats'][index])
            i['formats'].pop(index)
        else:
            pass
        index+=1

or if you want to delete main entries dicts (like :result[‘entries’]):

for i in result['entries']:
    index=0
    if i['ext'] != 'mp4':
        print("Info --- > Deleting: ",i['formats'][index])
        result['entries'].pop(index)
    else:
        pass
    index+=1

note: result is your json data.

Answered By: Clegane

I raise the same question @ ytdl-org here, and thanks to user dirkf I got the answer I needed: after modifying the json, you call make the request with process_ie_result, this is an example:

ydl = youtube_dl.YoutubeDL({'retries': 10})
result = ydl.extract_info(vurl, download=False)

Now, after modifying the json in result, you can download the video with:

new_result = ydl.process_ie_result(result, download=True)
Answered By: Gerardo
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.