hls-segmenter.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env python
  2. # upload to AWS S3 and clean up
  3. # author Roman Sereda
  4. # sereda.roman@gmail.com
  5. #
  6. # install dependenses
  7. # sudo pip install boto
  8. import json
  9. import os.path
  10. import logging
  11. import subprocess
  12. from boto.s3.connection import S3Connection
  13. from boto.s3.key import Key
  14. config_file = 'config.json'
  15. json_data = open(config_file)
  16. config = json.load(json_data)
  17. json_data.close()
  18. logging.basicConfig(filename=config['log'], level=logging.DEBUG)
  19. ###########################################################################
  20. def s3_file_upload(config, filename, keyname):
  21. conn = S3Connection(config['aws']["access_key"], config['aws']["secret_key"])
  22. mybucket = conn.get_bucket(config['aws']["s3bucket"]) # select bucket
  23. k = Key(mybucket) # select key
  24. k.key = keyname # named new key
  25. k.set_contents_from_filename(filename) # upload new file name
  26. k.set_acl('public-read') # set publis read access
  27. keylist = mybucket.list() # get list of files
  28. result = False
  29. ss = []
  30. for key in keylist:
  31. ss.append(key.name)
  32. if any(keyname in s for s in ss):
  33. logging.debug('s3_file_upload ' + 'Upload ' + keyname + "Completed")
  34. result = result | True
  35. rs = conn.close()
  36. return result
  37. def isVideo(ffprobe, filename):
  38. if os.path.isfile(filename):
  39. command = [ffprobe, "-v", "quiet", "-print_format", "json", "-show_format", "-show_streams", filename]
  40. process = subprocess.Popen(command, stdout=subprocess.PIPE)
  41. out, err = process.communicate()
  42. video_stat = json.loads(out)
  43. stat = []
  44. print(video_stat)
  45. if not len(video_stat) is 0:
  46. if 'streams' in video_stat:
  47. logging.debug('isVideo ' + 'tested ' + filename)
  48. if len(video_stat['streams']) >= 2:
  49. logging.debug('isVideo ' + 'tested the is Vidoe' + filename)
  50. return video_stat
  51. return False
  52. def s3_get_key_list(config):
  53. conn = S3Connection(config["aws"]["access_key"], config["aws"]["secret_key"])
  54. mybucket = conn.get_bucket(config["aws"]["s3bucket"])
  55. key_list = []
  56. for key in mybucket.list():
  57. key_list.append(key.name)
  58. rs = conn.close()
  59. return key_list
  60. def video_segmenter(ffmpeg, filepath, folder, stream_name):
  61. if os.path.isfile(filepath):
  62. command = [ffmpeg, "-re", "-i", filepath, "-map", "0", "-codec:v", "libx264", "-codec:a", "libfdk_aac",
  63. "-codec:s", "copy", "-flags", "-global_header", "-f", "segment", "-segment_list",
  64. folder + "playlist.m3u8", "-segment_time", "10", "-segment_format", "mpegts", folder + "out%05d.ts"]
  65. process = subprocess.Popen(command, stdout=subprocess.PIPE)
  66. out, err = process.communicate()
  67. print(out)
  68. def main(config):
  69. filelist = sorted(os.listdir(config["source"]))
  70. print(filelist)
  71. for filename in filelist:
  72. if isVideo(config["ffprobe"], config["source"] + filename):
  73. if filename.find('.') is -1:
  74. stream_name = filename.split('.', 1)
  75. else:
  76. stream_name = filename
  77. video_segmenter(config["ffmpeg"], config["source"] + filename, config["tmp"], stream_name)
  78. upload_list = sorted(os.listdir(config["tmp"]))
  79. if "playlist.m3u8" in upload_list and len(upload_list) > 2:
  80. for ufile in upload_list:
  81. logging.debug('main ' + 'procesed ' + " " + ufile)
  82. if s3_file_upload(config, config["tmp"] + ufile, stream_name + "/" + ufile):
  83. logging.debug('main ' + "Upload " + stream_name + "/" + ufile + "################")
  84. os.remove(config["tmp"] + ufile)
  85. print(s3_get_key_list(config))
  86. if __name__ == "__main__":
  87. main(config)