
import time
start = time.time()
import logging
import os, glob, sys
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

segment = str(sys.argv[1])
table_name="2m"
if os.path.isfile(table_name+"/2m_hls/"+segment) and ".ts" in segment:

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

    print("segment => "+segment)
    #clip = VideoFileClip("2m/2m_hls/"+segment)
    #duration = with_ffprobe("2m/2m_hls/"+segment)
    duration = 3
    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/2m_hls/"+segment+" -vf 'select=gt(scene\,0.10)' -vsync vfr -frame_pts true 2m/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)