summaryrefslogtreecommitdiff
path: root/hacks/make_dummy_files
blob: b2950dd01818e8c6d969a805c557e331d54ebfe0 (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/python3

import sys
import os
import hashlib
import ntpath
import tempfile
import shutil
import xml.etree.ElementTree as ET

if len(sys.argv) < 2:
    print('Syntax: %s <film>' % sys.argv[1])
    sys.exit(1)

metadata_xml = os.path.join(sys.argv[1], 'metadata.xml');
tree = ET.parse(metadata_xml)

def digest_head_tail(filename, size=1000000):
    m = hashlib.md5()
    f = open(filename, 'rb')
    m.update(f.read(size))
    f.seek(size, 2)
    m.update(f.read(size))
    f.close()
    return m.hexdigest() + str(os.path.getsize(filename))

def command(c):
    print(f"=> {c}")
    os.system(c)


try:
    os.makedirs(os.path.join(sys.argv[1], 'dummy'))
except:
    pass

root = tree.getroot()

for c in root.find('Playlist').findall('Content'):
    type = c.find('Type').text
    if type == 'DCP':
        # Find the ASSETMAP
        assetmap_file = None
        for p in c.findall('Path'):
            if os.path.basename(p.text) == 'ASSETMAP':
                assetmap_file = p.text

        assert(assetmap_file is not None)
        dir = os.path.dirname(assetmap_file)

        assets = {}

        assetmap = ET.parse(assetmap_file)
        ns = {'am': 'http://www.digicine.com/PROTO-ASDCP-AM-20040311#'}
        for a in assetmap.getroot().find('am:AssetList', ns).findall('am:Asset', ns):
            assets[a.find('am:Id', ns).text[9:]] = a.find('am:ChunkList', ns).find('am:Chunk', ns).find('am:Path', ns).text

        cpl_id = None
        for k, v in assets.iteritems():
            try:
                e = ET.parse(os.path.join(dir, v))
                if e.getroot().tag == '{http://www.digicine.com/PROTO-ASDCP-CPL-20040511#}CompositionPlaylist':
                    cpl_id = k
            except:
                pass

        assert(cpl_id is not None)
        cpl = ET.parse(os.path.join(dir, assets[cpl_id]))

        ns = {'cpl': 'http://www.digicine.com/PROTO-ASDCP-CPL-20040511#'}
        for r in cpl.find('cpl:ReelList', ns).findall('cpl:Reel', ns):
            for a in r.find('cpl:AssetList', ns).iter():
                if a.tag == '{%s}MainPicture' % ns['cpl']:
                    id = a.find('cpl:Id', ns).text[9:]
                    duration = int(a.find('cpl:IntrinsicDuration', ns).text)
                    black_png = tempfile.NamedTemporaryFile('wb', suffix='.png')
                    black_j2c = tempfile.NamedTemporaryFile('wb', suffix='.j2c')
                    os.system('convert -size 1998x1080 xc:black %s' % black_png.name)
                    os.system('image_to_j2k -i %s -o %s' % (black_png.name, black_j2c.name))
                    j2c_dir = tempfile.mkdtemp()
                    for i in range(0, duration):
                        shutil.copyfile(black_j2c.name, os.path.join(j2c_dir, '%06d.j2c' % i))
                    os.system('asdcp-wrap -a %s %s %s' % (id, j2c_dir, os.path.join(sys.argv[1], 'dummy', assets[id])))
                elif a.tag == '{%s}MainSound' % ns['cpl']:
                    wav = tempfile.NamedTemporaryFile('wb', suffix='.wav')
                    id = a.find('cpl:Id', ns).text[9:]
                    duration = int(a.find('cpl:IntrinsicDuration', ns).text)
                    edit_rate = int(a.find('cpl:EditRate', ns).text.split()[0])
                    os.system('sox -n -r 48000 -c 6 %s trim 0.0 %f' % (wav.name, float(duration) / edit_rate))
                    os.system('asdcp-wrap -a %s %s %s' % (id, wav.name, os.path.join(sys.argv[1], 'dummy', assets[id])))
    elif type == 'Sndfile':
        audio_frame_rate = int(c.find('AudioFrameRate').text)
        channels = int(c.find('AudioMapping').find('InputChannels').text)
        path = os.path.join(sys.argv[1], 'dummy', ntpath.basename(c.find('Path').text))
        audio_length = int(c.find('AudioLength').text)
        os.system('sox -n -r %d -c %d %s trim 0.0 %f' % (audio_frame_rate, channels, path, float(audio_length) / audio_frame_rate))
    elif type == 'FFmpeg':
        have_video = False
        have_audio = False
        path = os.path.join(sys.argv[1], 'dummy', ntpath.basename(c.find('Path').text))
        if c.find('VideoFrameRate') is not None:
            video_frame_rate = float(c.find('VideoFrameRate').text)
            video_length = int(c.find('VideoLength').text)
            have_video = True
            command(f"ffmpeg -t {float(video_length) / video_frame_rate} -s qcif -f rawvideo -pix_fmt rgb24 -r {video_frame_rate} -i /dev/zero video.mkv")
        if c.find('AudioStream') is not None:
            audio_channels = int(c.find('AudioStream').find('Mapping').find('InputChannels').text)
            audio_length = int(c.find('AudioStream').find('Length').text)
            audio_frame_rate = int(c.find('AudioStream').find('FrameRate').text)
            names = { 1: 'mono', 2: 'stereo', 3: '3.0', 4: '4.0', 5: '4.1', 6: '5.1', 7: '6.1', 8: '7.1' }
            have_audio = True
            print(f"audio_length={audio_length} frame_rate={audio_frame_rate}")
            command(f'sox -n -r 48000 -c {audio_channels} audio.wav trim 0.0 {audio_length}s')
        if have_video and have_audio:
            command(f"ffmpeg -i video.mkv -i audio.wav {path}")
        elif have_video:
            shutil.move("video.mkv", path)
        elif have_audio:
            shutil.move("audio.wav", path)
        try:
            os.remove("video.mkv")
            os.remove("audio.mkv")
        except:
            pass
        c.find('Path').text = path
        c.find('Digest').text = digest_head_tail(path)
    elif type == 'Image':
        width = int(c.find('VideoWidth').text)
        height = int(c.find('VideoHeight').text)
        path = os.path.join(sys.argv[1], 'dummy', ntpath.basename(c.find('Path').text))
        os.system('convert -size %dx%d xc:black "%s"' % (width, height, path))
    else:
        print('Skipped %s' % type)


shutil.move(metadata_xml, metadata_xml + '.bak')
r = open(metadata_xml, 'w')
r.write(ET.tostring(root).decode('UTF-8'))
r.close()