Home > Blockchain >  Stop Creating New List on Each Iteration
Stop Creating New List on Each Iteration

Time:12-08

The following code extracts the subtitle data from mkv files. I want the output in one list but it creates a list for each track. How can the code be changed to give one list containing all the tracks rather than a list for each track?

#!/usr/bin/env python3

import os
import re
import json
import subprocess
from itertools import chain

mkvmerge = "/usr/bin/mkvmerge"
keep_lang = "eng"

#############################################################################

def extract_subs(mkv_list):
    
    subtitle_tracks = []
    video_tracks = []
    audio_tracks = []
    extractList = []

    for file in mkv_list:

        ### Commandline auguments for extracting info about the mkv file
        command = [mkvmerge, "-i", "-F", "json", file]

        # Ask mkvmerge for the json info
        process = subprocess.run(command, capture_output=True, text=True, check=True)
        stdout = process.stdout

        ### Process the json response
        json__data = json.loads(stdout)

        tracks = json__data.get('tracks', [])

        ### find audio and subtitle tracks
        audio = []
        subtitle = []
        track_list = []

        for track in tracks:

            track['properties']['id'] = track['id']
            if track['type'] == 'audio':
                audio.append(track)
            elif track['type'] == 'subtitles':
                subtitle.append(track)

        # filter out files that don't need processing
        if len(audio) < 2 and len(subtitle) < 2:
            pass
            #print("\nNo extracted subs to process.", file)

        subtitle_keep = list(filter(lambda a: a ['properties']['language']==keep_lang, subtitle))

        for s in subtitle_keep:
            track_list.append(f"Track #{s['id']}: " f"{s['properties'].get('language')}" f" - " f"{s['codec']}: "   file)

        print (track_list)

output

['Track #2: eng - SubRip/SRT: /home/mp/torrents/test/Belfast (2021)/Belfast (2021).mkv', 'Track #3: eng - SubRip/SRT: /home/mp/torrents/test/Belfast (2021)/Belfast (2021).mkv']
['Track #2: eng - SubRip/SRT: /home/mp/torrents/test/The Rescue (2021)/The Rescue (2021).mkv']

desired output

['Track #2: eng - SubRip/SRT: /home/mp/torrents/test/Belfast (2021)/Belfast (2021).mkv', 'Track #3: eng - SubRip/SRT: /home/mp/torrents/test/Belfast (2021)/Belfast (2021).mkv', 'Track #2: eng - SubRip/SRT: /home/mp/torrents/test/The Rescue (2021)/The Rescue (2021).mkv']

CodePudding user response:

It seems like that your variable track_list has the wrong scope. Try moving it out of the scope of the for loop.

#!/usr/bin/env python3

import os
import re
import json
import subprocess
from itertools import chain

mkvmerge = "/usr/bin/mkvmerge"
keep_lang = "eng"

#############################################################################

def extract_subs(mkv_list):
    
    subtitle_tracks = []
    video_tracks = []
    audio_tracks = []
    extractList = []
    track_list = []

    for file in mkv_list:

        ### Commandline auguments for extracting info about the mkv file
        command = [mkvmerge, "-i", "-F", "json", file]

        # Ask mkvmerge for the json info
        process = subprocess.run(command, capture_output=True, text=True, check=True)
        stdout = process.stdout

        ### Process the json response
        json__data = json.loads(stdout)

        tracks = json__data.get('tracks', [])

        ### find audio and subtitle tracks
        audio = []
        subtitle = []

        for track in tracks:

            track['properties']['id'] = track['id']
            if track['type'] == 'audio':
                audio.append(track)
            elif track['type'] == 'subtitles':
                subtitle.append(track)

        # filter out files that don't need processing
        if len(audio) < 2 and len(subtitle) < 2:
            pass
            #print("\nNo extracted subs to process.", file)

        subtitle_keep = list(filter(lambda a: a ['properties']['language']==keep_lang, subtitle))

        for s in subtitle_keep:
            track_list.append(f"Track #{s['id']}: " f"{s['properties'].get('language')}" f" - " f"{s['codec']}: "   file)

        print (track_list)
  • Related