<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">import logging
import os, glob, sys
from importlib import reload
reload(logging)
import coloredlogs
import concurrent.futures
import json
import cv2
import time
from datetime import datetime, timedelta
import subprocess, json
import telegram

my_token = '5522894419:AAE9CeONlV2lfmUGqbXGZAHC-l1Z90TMuD4'

def send(msg, chat_id, token=my_token):
    """
    Send a message to a telegram user or group specified on chatId
    chat_id must be a number!
    """
    bot = telegram.Bot(token=token)
    bot.sendMessage(chat_id=chat_id, text=msg)

def compare_image(image_1, image_2):
    start_time = time.time()
    image_1=cv2.imread(image_1)
    image_2=cv2.imread(image_2)
    first_image_hist = cv2.calcHist([image_1], [0], None, [256], [0, 256])
    second_image_hist = cv2.calcHist([image_2], [0], None, [256], [0, 256])

    img_hist_diff = cv2.compareHist(first_image_hist, second_image_hist, cv2.HISTCMP_BHATTACHARYYA)
    img_template_probability_match = cv2.matchTemplate(first_image_hist, second_image_hist, cv2.TM_CCOEFF_NORMED)[0][0]
    img_template_diff = 1 - img_template_probability_match

    # taking only 10% of histogram diff, since it's less accurate than template method
    commutative_image_diff = (img_hist_diff * 0.50) + img_template_diff
    end_time = time.time()
    # print("Time taken to compare images in Seconds: {}".format(end_time - start_time))
    return commutative_image_diff

def with_ffprobe(filename):

    result = subprocess.check_output(
            f'ffprobe -v quiet -show_streams -select_streams v:0 -of json "{filename}"',
            shell=True).decode()
    fields = json.loads(result)['streams'][0]

    return float(fields['duration'])

    
def change_name(file,ref):
    res=ref.split('.')[0]
    res2=res[-3:]
    res1=res[:-3]
    plus_one=int(res2)+1
    name=res1+str(plus_one)
    os.rename('2m/2m_hls/'+file, '2m/2m_hls/'+name+'.ts')
    os.system("echo '2m/2m_hls/"+name+".ts'  &gt;&gt; result.m3u8")
    with open("all_files.json", "r+") as jsonFile6: #insert all changed ts videos
        all_names = json.load(jsonFile6)
    all_names['names']=name+'.ts'

    jsonFile6 = open("all_files.json", "w+")
    jsonFile6.write(json.dumps(all_names))
    jsonFile6.close()

def save_changed_file(filename):
    result='@'+filename
    #saving the cutted file with @ in json to not proccess as new ts file
    with open("ref.json", "r+") as jsonFile1:  #one we detect the fist jingle we stop for sometime
        data_ref = json.load(jsonFile1)
    #if result not in data_ref['name']:
    data_ref['name']=result

    #once we have posted we should add 1
    jsonFile1 = open("ref.json", "w+")
    jsonFile1.write(json.dumps(data_ref))
    jsonFile1.close()

def fix_flag(filename,flag):

    with open("all_files.json", "r+") as jsonFile6: #insert all changed ts videos
        all_names = json.load(jsonFile6)

    res=filename
    res2=res[-3:] # getting the last 3 nubmer of the filename
    res1=res[:-3] # getting everything except the last 3 number
    if res2[0]=='0':
        res2=res[-2:]
        res1=res[:-2]
    if flag=='2':
        li=[res1+str(i)+".ts" for i in range(int(res2)-1,int(res2)-10,-1)]  ## if leght ts change
        files_path=glob.glob("2m/2m_hls/*.ts")
        new_files=[file.split('/')[-1] for file in files_path]
        if all_names['names'] in new_files:
            new_files.remove(all_names['names'])
    if flag=='3':
        li=[res1+str(i)+".ts" for i in range(int(res2)-10,int(res2)-17,-1)]   ## if leght ts change
        files_path=glob.glob("2m/2m_hls/*.ts")
        new_files=[file.split('/')[-1] for file in files_path]
        if all_names['names'] in new_files:
            new_files.remove(all_names['names'])
 
    return [file for file in li if file in new_files][0]



def get_right_ts(filename,flag):

   # name=filename.split('/')[-1].split('_')[0]
    name=filename.split('_')[0]
    if flag=='1':
        return name+".ts"
    # folder_path = "2m/2m_hls/"#"/".join(filename.split('/')[:-1])+'/'
    # files_path = os.path.join(folder_path, '*.ts')
    # files = sorted(glob.iglob(files_path), key=os.path.getctime, reverse=True)
    # new_files=[file.split('/')[-1] for file in files ]
    # Index=new_files.index(name+".ts")
    if flag=='2':
        return fix_flag(name,flag)
    if flag=='3':
        return fix_flag(name,flag)


def check_ts_in_temp(name):

    ts_videos=[ts_video for ts_video in glob.glob("temp/*.ts")]
    new_videos=[video.split('/')[-1] for video in ts_videos]
    return 0 if name in new_videos else 1

# def get_latest_ts():

#     folder_path = "temp/"#"/".join(filename.split('/')[:-1])+'/'
#     files_path = os.path.join(folder_path, '*.ts')
#     files = sorted(glob.iglob(files_path), key=os.path.getctime, reverse=True)
#     new_files=[file.split('/')[-1] for file in files ]

#     return new_files[0].split('.')[0] if len(new_files)!=0 else ""

#


# def get_first_ts(val):

#     if right_list(val)!=first_file['name']:
#         return right_list(val)

#     else:
#         time.sleep(3)
#         return right_list(val)
</pre></body></html>