assembler.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import os
  2. import shutil
  3. import re
  4. import glob
  5. import subprocess
  6. import json
  7. import sys
  8. try:
  9. import pil
  10. import logger
  11. import helpers
  12. from constants import Constants
  13. except ImportError:
  14. from . import pil
  15. from . import logger
  16. from . import helpers
  17. from .constants import Constants
  18. """
  19. The content of this file was originally written by https://github.com/taengstagram
  20. The code has been edited for use in PyInstaLive.
  21. """
  22. def _get_file_index(filename):
  23. """ Extract the numbered index in filename for sorting """
  24. mobj = re.match(r'.+\-(?P<idx>[0-9]+)\.[a-z]+', filename)
  25. if mobj:
  26. return int(mobj.group('idx'))
  27. return -1
  28. def assemble(user_called=True, retry_with_zero_m4v=False):
  29. try:
  30. ass_json_file = pil.assemble_arg if pil.assemble_arg.endswith(".json") else pil.assemble_arg + ".json"
  31. ass_mp4_file = os.path.join(pil.dl_path, os.path.basename(ass_json_file).replace("_downloads", "").replace(".json", ".mp4"))
  32. ass_segment_dir = pil.assemble_arg if not pil.assemble_arg.endswith(".json") else pil.assemble_arg.replace(".json", "")
  33. broadcast_info = {}
  34. if not os.path.isdir(ass_segment_dir) or not os.listdir(ass_segment_dir):
  35. logger.error('The segment directory does not exist or does not contain any files: %s' % ass_segment_dir)
  36. logger.separator()
  37. return
  38. if not os.path.isfile(ass_json_file):
  39. logger.warn("No matching json file found for the segment directory, trying to continue without it.")
  40. ass_stream_id = os.listdir(ass_segment_dir)[0].split('-')[0]
  41. broadcast_info['id'] = ass_stream_id
  42. broadcast_info['broadcast_status'] = "active"
  43. broadcast_info['segments'] = {}
  44. else:
  45. with open(ass_json_file) as info_file:
  46. try:
  47. broadcast_info = json.load(info_file)
  48. except Exception as e:
  49. logger.warn("Could not decode json file, trying to continue without it.")
  50. ass_stream_id = os.listdir(ass_segment_dir)[0].split('-')[0]
  51. broadcast_info['id'] = ass_stream_id
  52. broadcast_info['broadcast_status'] = "active"
  53. broadcast_info['segments'] = {}
  54. if broadcast_info.get('broadcast_status', '') == 'post_live':
  55. logger.error('Video segment files from replay downloads cannot be assembled.')
  56. return
  57. stream_id = str(broadcast_info['id'])
  58. segment_meta = broadcast_info.get('segments', {})
  59. if segment_meta:
  60. all_segments = [
  61. os.path.join(ass_segment_dir, k)
  62. for k in broadcast_info['segments'].keys()]
  63. else:
  64. all_segments = list(filter(
  65. os.path.isfile,
  66. glob.glob(os.path.join(ass_segment_dir, '%s-*.m4v' % stream_id))))
  67. all_segments = sorted(all_segments, key=lambda x: _get_file_index(x))
  68. sources = []
  69. audio_stream_format = 'assembled_source_{0}_{1}_mp4.tmp'
  70. video_stream_format = 'assembled_source_{0}_{1}_m4a.tmp'
  71. video_stream = ''
  72. audio_stream = ''
  73. has_skipped_zero_m4v = False
  74. if not all_segments:
  75. logger.error("No video segment files have been found in the specified folder.")
  76. logger.separator()
  77. return
  78. else:
  79. logger.info("Assembling video segment files from specified folder: {}".format(ass_segment_dir))
  80. for segment in all_segments:
  81. segment = re.sub('\?.*$', '', segment)
  82. if not os.path.isfile(segment.replace('.m4v', '.m4a')):
  83. logger.warn('Audio segment not found: {0!s}'.format(segment.replace('.m4v', '.m4a')))
  84. continue
  85. if segment.endswith('-init.m4v'):
  86. logger.info('Replacing %s' % segment)
  87. segment = os.path.join(
  88. os.path.dirname(os.path.realpath(__file__)), 'repair', 'init.m4v')
  89. if segment.endswith('-0.m4v') and not retry_with_zero_m4v:
  90. has_skipped_zero_m4v = True
  91. continue
  92. video_stream = os.path.join(
  93. ass_segment_dir, video_stream_format.format(stream_id, len(sources)))
  94. audio_stream = os.path.join(
  95. ass_segment_dir, audio_stream_format.format(stream_id, len(sources)))
  96. file_mode = 'ab'
  97. with open(video_stream, file_mode) as outfile, open(segment, 'rb') as readfile:
  98. shutil.copyfileobj(readfile, outfile)
  99. with open(audio_stream, file_mode) as outfile, open(segment.replace('.m4v', '.m4a'), 'rb') as readfile:
  100. shutil.copyfileobj(readfile, outfile)
  101. if audio_stream and video_stream:
  102. sources.append({'video': video_stream, 'audio': audio_stream})
  103. for n, source in enumerate(sources):
  104. ffmpeg_binary = os.getenv('FFMPEG_BINARY', 'ffmpeg')
  105. cmd = [
  106. ffmpeg_binary, '-loglevel', 'warning', '-y',
  107. '-i', source['audio'],
  108. '-i', source['video'],
  109. '-c:v', 'copy', '-c:a', 'copy', ass_mp4_file]
  110. #fnull = open(os.devnull, 'w')
  111. fnull = None
  112. exit_code = subprocess.call(cmd, stdout=fnull, stderr=subprocess.STDOUT)
  113. if exit_code != 0:
  114. logger.warn("FFmpeg exit code not '0' but '{:d}'.".format(exit_code))
  115. if has_skipped_zero_m4v and not retry_with_zero_m4v:
  116. logger.binfo("*-0.m4v segment was detected but skipped, retrying to assemble video without "
  117. "skipping it.")
  118. os.remove(source['audio'])
  119. os.remove(source['video'])
  120. logger.separator()
  121. assemble(user_called, retry_with_zero_m4v=True)
  122. return
  123. else:
  124. logger.info('The video file has been generated: %s' % os.path.basename(ass_mp4_file))
  125. os.remove(source['audio'])
  126. os.remove(source['video'])
  127. if user_called:
  128. logger.separator()
  129. except Exception as e:
  130. logger.error("An error occurred: {:s}".format(str(e)))