import time
start = time.time()
import logging
import os, glob, sys, random
from importlib import reload
reload(logging)
import coloredlogs
import concurrent.futures
from functions import *
#from handling_json_file import *
import json
import cv2
from datetime import datetime, timedelta
#from moviepy.editor import VideoFileClip
import telegram
import asyncio
from telegram import Bot
# import tracemalloc

# # Enable tracemalloc
# tracemalloc.start()

segment = "test.ts" #str(sys.argv[1])

#if os.path.isfile(table_name+"_test/2m_hls/"+segment) and ".ts" in segment and segment !=all_names['names'] and '@' not in segment and 'jingle' not in segment:

    #if level2['end']<current_time and data_stat['situation']

print("segment => "+segment)
#clip = VideoFileClip("2m/2m_hls/"+segment)
duration = with_ffprobe("2m_test/2m_hls/"+segment)
# duration = 6
print('the duration is :',duration)
ts = time.time()
ts = "."+str(ts).split('.')[0]
now = str(segment).split('.')[0]

home = os.path.expanduser("~")
ffmpeg = 'ffmpeg'
cwd = os.getcwd()

# video = 25 frame par second

cmd ="ffmpeg -y -i 2m_test/2m_hls/"+segment+" -vf 'select=gt(scene\,0.10)' -vsync vfr -frame_pts true 2m_test/iframe_live/"+now+"_%d_"+str(duration)+".png"
#cmd = "ffmpeg -y -i 2m_hls/"+segment+" -vf 'select=gt(scene\,0.10)' -vsync vfr -frame_pts true iframes_live/"+now+str(ts)+"_%d_"+str(duration)+".png"
# cmd = "ffmpeg -y -i 2m_hls/"+segment+" -vf \"select='eq(pict_type,PICT_TYPE_I)'\" -vsync vfr -frame_pts true iframes_live/"+now+str(ts)+"_%d_"+str(duration)+".png"
print(cmd)
# python3 iframe_scanner.py $file
os.system(cmd)
#send("from server 2 there is similarity between "+"test"+" and "+"test" +" with results = "+str(0),"-1001778433367", token=my_token)
print('done §')