Skip to content

Instantly share code, notes, and snippets.

@Luavis
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Luavis/1fcb88356efb2b14bd20 to your computer and use it in GitHub Desktop.
Save Luavis/1fcb88356efb2b14bd20 to your computer and use it in GitHub Desktop.
pid install pycaption
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from pycaption import SAMIReader, SRTWriter
import sys
import os
from chardet.universaldetector import UniversalDetector
def main():
if len(sys.argv) < 1:
print("./smi_srt.py [smi file name].smi")
else:
sub_path = sys.argv[1]
u = UniversalDetector()
sami = open(sub_path, 'rb').read()
u.feed(sami)
u.close()
encoding_name = u.result['encoding'].lower()
sami_content = ""
if not encoding_name == 'utf-8':
uni_sami = unicode(sami, encoding_name)
sami_content = uni_sami.encode('utf8')
else:
sami_content = str(sami)
pycaps = SAMIReader().read(sami_content)
srt_content = SRTWriter().write(pycaps)
new_path = os.path.splitext(sub_path)[0] + '.srt'
open(new_path, 'w+').write(srt_content)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment