mkv_split.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import os
  2. import sys
  3. import subprocess
  4. import shutil
  5. def load_drop_list():
  6. if not os.path.exists('drop.list'):
  7. print 'drop.list not found'
  8. return set()
  9. else:
  10. with open('drop.list') as f:
  11. res = {int(x) for x in f.readlines()}
  12. print 'Loaded %i entries from drop.list' % len(res)
  13. return res
  14. def load_csv(filename):
  15. scenes = []
  16. with open(filename) as f:
  17. lines = f.readlines()
  18. for line in lines[2:]:
  19. scene, frame, timecode, start_time_s, duration_s = line.split(',')
  20. scenes.append((int(scene)+1, timecode))
  21. return scenes
  22. def load_enabled_scenes(basename, num_total_scenes):
  23. res = set()
  24. for i in range(num_total_scenes):
  25. if os.path.exists('%s.Scene-%i-IN.jpg' % (basename, i+1)):
  26. res.add(i+1)
  27. return res
  28. def detect(input_file, threshold):
  29. call = ['scenedetect', '-i', input_file, '-co', '%s.csv' % input_file, '-d', 'content', '-si', '-t', threshold]
  30. raw_input('Press enter to execute %s ...' % call)
  31. subprocess.call(call)
  32. if raw_input('Change threshold? [y/N]') == 'y':
  33. threshold = raw_input('Enter threshold: ')
  34. return detect(input_file, threshold)
  35. print 'Done. Find the scene screen captures and delete any false positive IN.jpg files, then run the split command when done.'
  36. def copy_xmp_file(input_file):
  37. xmp_file = '%s.xmp' % input_file
  38. if not os.path.exists(xmp_file):
  39. return
  40. cwd = os.getcwd()
  41. for f in os.listdir(cwd):
  42. if f.startswith(input_file.replace('.mkv', '')) and f.endswith('.mkv') and f != input_file:
  43. shutil.copyfile(xmp_file, '%s.xmp' % f)
  44. def split(input_file):
  45. scenes = load_csv('%s.csv' % input_file)
  46. enabled_scenes = load_enabled_scenes(input_file, len(scenes))
  47. drops = load_drop_list()
  48. to_delete = set()
  49. cuts = []
  50. for scene, timecode in scenes:
  51. if scene in enabled_scenes:
  52. cuts.append(timecode)
  53. print 'mkvmverge scene %i: scenedetect scene %i (%s)' % (len(cuts)+1, scene, timecode)
  54. if scene in drops:
  55. to_delete.add('%s-split-%03d.mkv' % (input_file.replace('.mkv', ''), len(cuts)+1))
  56. print 'delete: %s' % to_delete
  57. call = ['mkvmerge', '-o', input_file.replace('.mkv', '-split.mkv'), '--split', 'timecodes:%s' % ','.join(cuts), input_file]
  58. raw_input('Press enter to execute %s ...' % call)
  59. subprocess.call(call)
  60. for f in to_delete:
  61. if os.path.exists(f):
  62. os.remove(f)
  63. else:
  64. print 'Could not find %s' % f
  65. copy_xmp_file(input_file)
  66. print 'Done.'
  67. if __name__ == '__main__':
  68. if len(sys.argv) < 2 or sys.argv[1] not in ('detect', 'split'):
  69. print 'Usage: mkv_split.py [detect|split] <input file> [<detection threshold>]'
  70. if sys.argv[1] == 'detect':
  71. detect(sys.argv[2], sys.argv[3])
  72. elif sys.argv[1] == 'split':
  73. split(sys.argv[2])