#!/usr/bin/python3

import sys
import os
import re
import json
import subprocess


def get_track_list(url):
    out = subprocess.check_output(['youtube-dl', '--dump-json', url])
    out_dict = json.loads(out.decode('utf-8')[:-1])
    chapters = out_dict.get('chapters')
    if chapters:
        def ffmpeg_time(seconds):
            minutes, seconds = divmod(int(seconds), 60)
            hours, minutes = divmod(minutes, 60)
            return '{:02}:{:02}:{:02}'.format(hours, minutes, seconds)

        return [{'start_time': ffmpeg_time(c.get('start_time')),
                 'end_time': ffmpeg_time(c.get('end_time')),
                 'title': c.get('title')}
                for c in chapters]

    regex = r'([0-9]{1,3}:[0-9]{1,2}:?[0-9]{0,2})\s*-\s*([0-9]{1,3}:[0-9]{1,2}:?[0-9]{0,2})\s+(.*?)$'
    track_list = []
    for line in out_dict.get('description').splitlines():
        m = re.search(regex, line)
        if m:
            track_list.append(
                {'start_time': m.group(1), 'end_time': m.group(2), 'title': m.group(3)})
    return track_list


def split_audio(file, track_list):
    if not track_list:
        return

    cmd_format = 'ffmpeg -i "{}" -acodec copy -ss {} -to {} "{}"'
    fname, fext = os.path.splitext(file)
    try:
        os.mkdir(fname)
    except FileExistsError:
        pass
    for track in track_list:
        cmd = cmd_format.format(file, track.get('start_time'),
                                track.get('end_time'),
                                os.path.join(fname, track.get('title')+fext))
        subprocess.call(cmd, shell=True)


def main():
    if len(sys.argv) < 1:
        exit(1)

    track_list = get_track_list(sys.argv[1])
    if not track_list:
        exit(1)

    split_audio(sys.argv[2], track_list)


if __name__ == "__main__":
    main()

fae405b1661b720595168e9a25edbaeafc318c08
<mask token>


def get_track_list(url):
    out = subprocess.check_output(['youtube-dl', '--dump-json', url])
    out_dict = json.loads(out.decode('utf-8')[:-1])
    chapters = out_dict.get('chapters')
    if chapters:

        def ffmpeg_time(seconds):
            minutes, seconds = divmod(int(seconds), 60)
            hours, minutes = divmod(minutes, 60)
            return '{:02}:{:02}:{:02}'.format(hours, minutes, seconds)
        return [{'start_time': ffmpeg_time(c.get('start_time')), 'end_time':
            ffmpeg_time(c.get('end_time')), 'title': c.get('title')} for c in
            chapters]
    regex = (
        '([0-9]{1,3}:[0-9]{1,2}:?[0-9]{0,2})\\s*-\\s*([0-9]{1,3}:[0-9]{1,2}:?[0-9]{0,2})\\s+(.*?)$'
        )
    track_list = []
    for line in out_dict.get('description').splitlines():
        m = re.search(regex, line)
        if m:
            track_list.append({'start_time': m.group(1), 'end_time': m.
                group(2), 'title': m.group(3)})
    return track_list


def split_audio(file, track_list):
    if not track_list:
        return
    cmd_format = 'ffmpeg -i "{}" -acodec copy -ss {} -to {} "{}"'
    fname, fext = os.path.splitext(file)
    try:
        os.mkdir(fname)
    except FileExistsError:
        pass
    for track in track_list:
        cmd = cmd_format.format(file, track.get('start_time'), track.get(
            'end_time'), os.path.join(fname, track.get('title') + fext))
        subprocess.call(cmd, shell=True)


<mask token>

<mask token>


def get_track_list(url):
    out = subprocess.check_output(['youtube-dl', '--dump-json', url])
    out_dict = json.loads(out.decode('utf-8')[:-1])
    chapters = out_dict.get('chapters')
    if chapters:

        def ffmpeg_time(seconds):
            minutes, seconds = divmod(int(seconds), 60)
            hours, minutes = divmod(minutes, 60)
            return '{:02}:{:02}:{:02}'.format(hours, minutes, seconds)
        return [{'start_time': ffmpeg_time(c.get('start_time')), 'end_time':
            ffmpeg_time(c.get('end_time')), 'title': c.get('title')} for c in
            chapters]
    regex = (
        '([0-9]{1,3}:[0-9]{1,2}:?[0-9]{0,2})\\s*-\\s*([0-9]{1,3}:[0-9]{1,2}:?[0-9]{0,2})\\s+(.*?)$'
        )
    track_list = []
    for line in out_dict.get('description').splitlines():
        m = re.search(regex, line)
        if m:
            track_list.append({'start_time': m.group(1), 'end_time': m.
                group(2), 'title': m.group(3)})
    return track_list


def split_audio(file, track_list):
    if not track_list:
        return
    cmd_format = 'ffmpeg -i "{}" -acodec copy -ss {} -to {} "{}"'
    fname, fext = os.path.splitext(file)
    try:
        os.mkdir(fname)
    except FileExistsError:
        pass
    for track in track_list:
        cmd = cmd_format.format(file, track.get('start_time'), track.get(
            'end_time'), os.path.join(fname, track.get('title') + fext))
        subprocess.call(cmd, shell=True)


def main():
    if len(sys.argv) < 1:
        exit(1)
    track_list = get_track_list(sys.argv[1])
    if not track_list:
        exit(1)
    split_audio(sys.argv[2], track_list)


<mask token>

<mask token>


def get_track_list(url):
    out = subprocess.check_output(['youtube-dl', '--dump-json', url])
    out_dict = json.loads(out.decode('utf-8')[:-1])
    chapters = out_dict.get('chapters')
    if chapters:

        def ffmpeg_time(seconds):
            minutes, seconds = divmod(int(seconds), 60)
            hours, minutes = divmod(minutes, 60)
            return '{:02}:{:02}:{:02}'.format(hours, minutes, seconds)
        return [{'start_time': ffmpeg_time(c.get('start_time')), 'end_time':
            ffmpeg_time(c.get('end_time')), 'title': c.get('title')} for c in
            chapters]
    regex = (
        '([0-9]{1,3}:[0-9]{1,2}:?[0-9]{0,2})\\s*-\\s*([0-9]{1,3}:[0-9]{1,2}:?[0-9]{0,2})\\s+(.*?)$'
        )
    track_list = []
    for line in out_dict.get('description').splitlines():
        m = re.search(regex, line)
        if m:
            track_list.append({'start_time': m.group(1), 'end_time': m.
                group(2), 'title': m.group(3)})
    return track_list


def split_audio(file, track_list):
    if not track_list:
        return
    cmd_format = 'ffmpeg -i "{}" -acodec copy -ss {} -to {} "{}"'
    fname, fext = os.path.splitext(file)
    try:
        os.mkdir(fname)
    except FileExistsError:
        pass
    for track in track_list:
        cmd = cmd_format.format(file, track.get('start_time'), track.get(
            'end_time'), os.path.join(fname, track.get('title') + fext))
        subprocess.call(cmd, shell=True)


def main():
    if len(sys.argv) < 1:
        exit(1)
    track_list = get_track_list(sys.argv[1])
    if not track_list:
        exit(1)
    split_audio(sys.argv[2], track_list)


if __name__ == '__main__':
    main()

import sys
import os
import re
import json
import subprocess


def get_track_list(url):
    out = subprocess.check_output(['youtube-dl', '--dump-json', url])
    out_dict = json.loads(out.decode('utf-8')[:-1])
    chapters = out_dict.get('chapters')
    if chapters:

        def ffmpeg_time(seconds):
            minutes, seconds = divmod(int(seconds), 60)
            hours, minutes = divmod(minutes, 60)
            return '{:02}:{:02}:{:02}'.format(hours, minutes, seconds)
        return [{'start_time': ffmpeg_time(c.get('start_time')), 'end_time':
            ffmpeg_time(c.get('end_time')), 'title': c.get('title')} for c in
            chapters]
    regex = (
        '([0-9]{1,3}:[0-9]{1,2}:?[0-9]{0,2})\\s*-\\s*([0-9]{1,3}:[0-9]{1,2}:?[0-9]{0,2})\\s+(.*?)$'
        )
    track_list = []
    for line in out_dict.get('description').splitlines():
        m = re.search(regex, line)
        if m:
            track_list.append({'start_time': m.group(1), 'end_time': m.
                group(2), 'title': m.group(3)})
    return track_list


def split_audio(file, track_list):
    if not track_list:
        return
    cmd_format = 'ffmpeg -i "{}" -acodec copy -ss {} -to {} "{}"'
    fname, fext = os.path.splitext(file)
    try:
        os.mkdir(fname)
    except FileExistsError:
        pass
    for track in track_list:
        cmd = cmd_format.format(file, track.get('start_time'), track.get(
            'end_time'), os.path.join(fname, track.get('title') + fext))
        subprocess.call(cmd, shell=True)


def main():
    if len(sys.argv) < 1:
        exit(1)
    track_list = get_track_list(sys.argv[1])
    if not track_list:
        exit(1)
    split_audio(sys.argv[2], track_list)


if __name__ == '__main__':
    main()

#!/usr/bin/python3

import sys
import os
import re
import json
import subprocess


def get_track_list(url):
    out = subprocess.check_output(['youtube-dl', '--dump-json', url])
    out_dict = json.loads(out.decode('utf-8')[:-1])
    chapters = out_dict.get('chapters')
    if chapters:
        def ffmpeg_time(seconds):
            minutes, seconds = divmod(int(seconds), 60)
            hours, minutes = divmod(minutes, 60)
            return '{:02}:{:02}:{:02}'.format(hours, minutes, seconds)

        return [{'start_time': ffmpeg_time(c.get('start_time')),
                 'end_time': ffmpeg_time(c.get('end_time')),
                 'title': c.get('title')}
                for c in chapters]

    regex = r'([0-9]{1,3}:[0-9]{1,2}:?[0-9]{0,2})\s*-\s*([0-9]{1,3}:[0-9]{1,2}:?[0-9]{0,2})\s+(.*?)$'
    track_list = []
    for line in out_dict.get('description').splitlines():
        m = re.search(regex, line)
        if m:
            track_list.append(
                {'start_time': m.group(1), 'end_time': m.group(2), 'title': m.group(3)})
    return track_list


def split_audio(file, track_list):
    if not track_list:
        return

    cmd_format = 'ffmpeg -i "{}" -acodec copy -ss {} -to {} "{}"'
    fname, fext = os.path.splitext(file)
    try:
        os.mkdir(fname)
    except FileExistsError:
        pass
    for track in track_list:
        cmd = cmd_format.format(file, track.get('start_time'),
                                track.get('end_time'),
                                os.path.join(fname, track.get('title')+fext))
        subprocess.call(cmd, shell=True)


def main():
    if len(sys.argv) < 1:
        exit(1)

    track_list = get_track_list(sys.argv[1])
    if not track_list:
        exit(1)

    split_audio(sys.argv[2], track_list)


if __name__ == "__main__":
    main()
