Пример #1
0
def encrypt(code):
    if not _('ENCRYPTION') == 'YES':
        return code

    for file in tsfiles(code):
        if file.startswith('enc.'):
            continue

        print('Encrypting %s to enc.%s ... ' % (file, file), end='')
        key = exec(['openssl', 'rand', '16']).hex()
        iv = execstr(['openssl', 'rand', '-hex', '16'])
        exec([
            'openssl', 'aes-128-cbc', '-e', '-in', file, '-out',
            'enc.%s' % file, '-p', '-nosalt', '-iv', iv, '-K', key
        ])

        key_id = api('POST', 'key', {'iv': iv, 'key': key})
        if not key_id:
            print('failed')
            open('out.m3u8', 'w').write(code)
            exit()

        print('done')
        code = re.sub(
            '(#EXTINF:.+$[\\r\\n]+^%s$)' % file,
            '#EXT-X-KEY:METHOD=AES-128,URI="%s/play/%s.key",IV=0x%s\n\\1' %
            (_('APIURL'), key_id, iv), code, 1, re.M)
        code = code.replace(file, 'enc.%s' % file)

    open('out.m3u8', 'w').write(code)
    return code
Пример #2
0
def video_codec(file):
    codecs = execstr([
        'ffprobe', '-v', 'error', '-select_streams', 'v:0', '-show_entries',
        'stream=codec_name', '-of', 'default=noprint_wrappers=1:nokey=1', file
    ])
    return 'h264' if set(codecs.split('\n')).difference({'h264'}) else 'copy'
Пример #3
0
def bit_rate(file):
    return int(
        execstr([
            'ffprobe', '-v', 'error', '-show_entries', 'format=bit_rate',
            '-of', 'default=noprint_wrappers=1:nokey=1', file
        ]))
Пример #4
0
def encrypt(code):
  if _('ENCRYPTION_VERSION') == 'V1':
    print('ENCRYPTION_VERSION:%s' % _('ENCRYPTION_VERSION'))
    head = read_bytes('../dangdai-32x32.png')
    head1 = head[0:-12]
    head2 = head[-12:]
    tmpdir = os.getcwd()+'/tmp'
    if not os.path.exists(tmpdir):
      os.mkdir(tmpdir)
    os.chdir(tmpdir)
    for file in tsfiles(code):
      portion = os.path.splitext(file)
      if portion[1] == ".ts":
        newName = portion[0] + ".png"
        if os.path.isfile(newName):
          code = code.replace(file, newName)
          continue
        segment = read_bytes('../' + file)
        segment = zlib.compress(segment)
        done_segments = head1 + segment + head2
        open(newName, 'wb').write(done_segments)
        code = code.replace(file, newName)
      #break
    #os.chdir('../')

  if _('ENCRYPTION_VERSION') == 'V2':
    print('ENCRYPTION_VERSION:%s' % _('ENCRYPTION_VERSION'))
    tmpdir = os.getcwd()+'/tmp'
    if not os.path.exists(tmpdir):
      os.mkdir(tmpdir)
    os.chdir(tmpdir)

    for file in tsfiles(code):
      # 1,获取切片大小
      filesize = os.path.getsize('../' +file)+64
      wh=math.ceil(math.sqrt(filesize/3))*4
      # wh=math.ceil(math.sqrt(filesize*3))
      print('切片大小:%s,图片尺寸:%s' % (filesize,wh))
      data = open('../' + file, "rb").read()
      data = zlib.compress(data)
      print('压缩后切片大小:%s' % (len(data)))

      portion = os.path.splitext(file)
      newName = portion[0] + ".png"

      if os.path.isfile(newName):
        code = code.replace(file, newName)
        continue
      # 2,生成图片
      new_img_file_name = new_image(wh, wh, portion[0], show_image=False)
      print('生成图片:%s' % (new_img_file_name))

      # 3,隐写
      in_img = cv2.imread(new_img_file_name)
      steg = LSBSteg(in_img)
      starttime = time.time()
      res = steg.encode_binary(data)
      duration = time.time()-starttime
      print('隐写完成时间:%s' % (duration))
      cv2.imwrite(newName, res)
      print('隐写完成:%s' % (newName))

      # embed(new_img_file_name, '../' + file)
      # 4,替换
      code = code.replace(file, newName)

      """
      # 1,获取切片大小
      filesize = os.path.getsize('../' + file) + 64
      wh = math.ceil(math.sqrt(filesize / 3))
      print('切片大小:%s,图片尺寸:%s' % (filesize, wh))
      data = open('../' + file, "rb").read()
      new_img_file_name = r'%s_%s_%s.png' % (wh, wh, md5(data))
      if os.path.isfile('out' + new_img_file_name):
        code = code.replace(file, 'out' + new_img_file_name)
        continue
      # 2,生成图片
      new_image(wh, wh, md5(data), show_image=False)
      print('生成图片:%s' % (new_img_file_name))

      # 3,隐写
      in_img = cv2.imread(new_img_file_name)
      steg = LSBSteg(in_img)
      starttime = time.time()
      res = steg.encode_binary(data)
      duration = time.time() - starttime
      print('隐写完成时间:%s' % (duration))
      cv2.imwrite('out' + new_img_file_name, res)
      print('隐写完成:%s' % ('out' + new_img_file_name))
      # embed(new_img_file_name, '../' + file)
      # 4,替换
      code = code.replace(file, 'out' + new_img_file_name)
      
      """


  if not _('ENCRYPTION') == 'YES':
    return code

  for file in tsfiles(code):
    if file.startswith('enc.'):
      continue

    print('Encrypting %s to enc.%s ... ' % (file, file), end='')
    key = exec(['openssl','rand','16']).hex()
    iv  = execstr(['openssl','rand','-hex','16'])
    exec(['openssl','aes-128-cbc','-e','-in',file,'-out','enc.%s' % file,'-p','-nosalt','-iv',iv,'-K',key])

    key_id = api('POST', 'key', data={'iv': iv, 'key': key})
    if not key_id:
      print('failed')
      open('out.m3u8', 'w').write(code)
      exit()

    print('done')
    code = re.sub('(#EXTINF:.+$[\\r\\n]+^%s$)' % file, '#EXT-X-KEY:METHOD=AES-128,URI="%s/play/%s.key",IV=0x%s\n\\1' % (_('APIURL'), key_id, iv), code, 1, re.M)
    code = code.replace(file, 'enc.%s' % file)

  open('out.m3u8', 'w').write(code)
  return code
Пример #5
0
def video_duration(file):
    return float(
        execstr([
            'ffprobe', '-v', 'error', '-show_entries', 'format=duration',
            '-of', 'default=noprint_wrappers=1:nokey=1', file
        ]))