Port make_dummy_files to Python3 and improve it in various ways.
authorCarl Hetherington <cth@carlh.net>
Thu, 2 Dec 2021 19:18:54 +0000 (20:18 +0100)
committerCarl Hetherington <cth@carlh.net>
Thu, 2 Dec 2021 19:18:54 +0000 (20:18 +0100)
hacks/make_dummy_files

index 3dce41cf8d19118dc9e7ab5b5d5857d3d5cb72ae..b2950dd01818e8c6d969a805c557e331d54ebfe0 100755 (executable)
@@ -1,22 +1,22 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 
 import sys
 import os
+import hashlib
 import ntpath
 import tempfile
 import shutil
-import md5
 import xml.etree.ElementTree as ET
 
 if len(sys.argv) < 2:
-    print 'Syntax: %s <film>' % sys.argv[1]
+    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 = md5.new()
+    m = hashlib.md5()
     f = open(filename, 'rb')
     m.update(f.read(size))
     f.seek(size, 2)
@@ -24,6 +24,10 @@ def digest_head_tail(filename, size=1000000):
     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'))
@@ -74,7 +78,6 @@ for c in root.find('Playlist').findall('Content'):
                     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()
-                    print j2c_dir
                     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])))
@@ -92,19 +95,33 @@ for c in root.find('Playlist').findall('Content'):
         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':
-        video_cmd = ''
-        audio_cmd = ''
+        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)
-            video_cmd = '-s qcif -f rawvideo -pix_fmt rgb24 -r %d -i /dev/zero' % video_frame_rate
+            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' }
-            audio_cmd = '-f lavfi -i anullsrc=channel_layout=%s:sample_rate=%d' % (names[audio_channels], audio_frame_rate)
-        os.system('ffmpeg -t %f %s %s -shortest "%s"' % (float(video_length) / video_frame_rate, video_cmd, audio_cmd, path))
+            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':
@@ -113,11 +130,11 @@ for c in root.find('Playlist').findall('Content'):
         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
+        print('Skipped %s' % type)
 
 
 shutil.move(metadata_xml, metadata_xml + '.bak')
 r = open(metadata_xml, 'w')
-r.write(ET.tostring(root))
+r.write(ET.tostring(root).decode('UTF-8'))
 r.close()