unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
blob 9a6a73b65226327c7665a87bc7a14c802fb53eb3 1893 bytes (raw)
name: talks/jres-2022/demo/mp4/do-chapters.py 	 # note: path name is non-authoritative(*)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
 
#!/bin/env python3

import re

def read_chapters(filename):
    print("Read chapters from {}...".format(filename), sep=" ", end="")

    with open(filename, 'r') as f:
        chapters = list()
        for line in f:
            try:
                x = re.match(r"(\d{2}):(\d{2}) (.*)", line)
            except:
                print("Error: excepted format 'XY:uv The title'")
                return

            mins = int(x.group(1))
            secs = int(x.group(2))
            title = x.group(3)

            minutes = mins
            seconds = secs + (minutes * 60)
            timestamp = (seconds * 1000)
            chap = {
                "title": title,
                "time": timestamp
            }
            chapters.append(chap)

    print("done.")
    return chapters


def generate_meta(chapters, offset=0):
    print("Generate meta...", sep=" ", end="")

    text = ""
    for i in range(len(chapters)-1): # Require unnecessary mm:ss Stop
        chap = chapters[i]
        title = chap['title']
        start = chap['time'] + offset
        end = chapters[i+1]['time']-1 + offset
        text += f"""
[CHAPTER]
TIMEBASE=1/1000
START={start}
END={end}
title={title}
"""

    print("done.")
    return text, end

if __name__ == "__main__":
    import sys
    try:
        offset = 0
        for i, filename in enumerate(sys.argv[1:]):
            chapters= read_chapters(filename)
            text, offset = generate_meta(chapters, offset)
            if len(sys.argv) > 2:
                out, perm = "merged.meta", 'a'
                if i == 0:
                    with open(out, 'w') as f:
                        f.write('')
            else:
                out, perm = filename + '.meta', 'w'
            with open(out, perm) as f:
                f.write(text)
            print("Wrote {}".format(out))
    except:
        print("Error.")
        sys.exit(1)

debug log:

solving 9a6a73b ...
found 9a6a73b in https://yhetil.org/guix-patches/20220620035132.3461-1-zimon.toutoune@gmail.com/

applying [1/1] https://yhetil.org/guix-patches/20220620035132.3461-1-zimon.toutoune@gmail.com/
diff --git a/talks/jres-2022/demo/mp4/do-chapters.py b/talks/jres-2022/demo/mp4/do-chapters.py
new file mode 100644
index 0000000..9a6a73b

Checking patch talks/jres-2022/demo/mp4/do-chapters.py...
Applied patch talks/jres-2022/demo/mp4/do-chapters.py cleanly.

index at:
100644 9a6a73b65226327c7665a87bc7a14c802fb53eb3	talks/jres-2022/demo/mp4/do-chapters.py

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).