Skip to content

Instantly share code, notes, and snippets.

@aldoridhoni
Created April 15, 2018 16:52
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aldoridhoni/8a2dfb323baf94f30c7f31d106f14c4d to your computer and use it in GitHub Desktop.
Save aldoridhoni/8a2dfb323baf94f30c7f31d106f14c4d to your computer and use it in GitHub Desktop.
Convert MOD video to mp4 and read some metadata from MOI file.
#!/bin/sh
for vid in *.MOD; do python3 convert_mod.py $vid; done
#!/usr/bin/env python3
import read_moi
import sys, os, shlex
from subprocess import call
# ffmpeg -i "" -codec copy -metadata title=2016-09-20T21:30:00 -metadata year=2016 -aspect 16:9 out.mp4
AUTHOR = 'Aldo Ridhoni'
QUIET = True
def mod2moi(filename):
return os.path.splitext(filename)[0] + '.MOI'
def get_metadata(filename):
metadata = dict()
with open(filename, 'rb') as binary_file:
metadata['date'] = read_moi.read_date(binary_file)
_, metadata['ratio'] = read_moi.read_aspectratio(binary_file)
return metadata
def main():
program = "ffmpeg -hide_banner"
input_file = "-i '%s'" % sys.argv[1]
codec = "-codec copy"
author = "-metadata 'author=%s'" % AUTHOR
metadata = get_metadata(mod2moi(sys.argv[1]))
title = metadata['date'].strftime('-metadata \'title=%Y-%m-%dT%H:%M:%S\'')
year = metadata['date'].strftime('-metadata \'year=%Y\'')
aspect = "-aspect %s" % metadata['ratio']
output = metadata['date'].strftime('%Y%m%d_%H%M%S.mp4')
args = " ".join((program, input_file, codec, author, title, year, aspect, output))
args = shlex.split(args)
print(args)
call(args)
if __name__ == "__main__":
main()
#!/usr/bin/env python3
import sys
from datetime import datetime
def seek_int(binary, seek, length):
binary.seek(seek, 0)
return int.from_bytes(binary.read(length), 'big')
def read_date(binary_file):
binary_file.seek(0, 0)
version = binary_file.read(2)
if version == b'V6':
year = seek_int(binary_file, 6, 2)
month = seek_int(binary_file, 8, 1)
day = seek_int(binary_file, 9, 1)
hour = seek_int(binary_file, 10, 1)
minutes = seek_int(binary_file, 11, 1)
miliseconds = seek_int(binary_file, 12, 2)
seconds = miliseconds // 1000
return datetime(year, month, day, hour, minutes, seconds)
return False
def read_duration(binary_file):
duration_ms = seek_int(binary_file, 14, 4)
return duration_ms // 1000
def read_aspectratio(binary_file):
binary_file.seek(128, 0)
data = binary_file.read(1).hex()
if data[:1] is "5":
system = "PAL"
elif data[:1] is "4":
system = "NTSC"
if data[1:2] in ("0", "1"):
ratio = "4:3"
elif data[1:2] in ("4", "5"):
ratio = "16:9"
return system, ratio
if __name__ == '__main__':
with open(sys.argv[1], 'rb') as binary_file:
date = read_date(binary_file)
duration = read_duration(binary_file)
system, ratio = read_aspectratio(binary_file)
if date:
print(date.strftime('File created at %d %b %Y %H:%M:%S'))
print("Duration: %s seconds" % duration)
print("System: %s, Ratio: %s" % (system, ratio))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment