# Path: utils/io_handler.py
# def get_subdirectory_files(dir, depth=1):
# def files_from_directory(dir_str, file_type):
# def file_paths_from_directory(dir_str, file_type):
# def get_filename_and_filetype_from_path(path):
# def get_metadata_dict_as_bytes(value):
# def get_video_id_from_path(path_str, type=None):
# def shuffle_files_in_list(paths_list, seed=5):
# def shuffle_files_in_list_from_categories(paths_list, categories, metadata_path, type='youtube8m', seed=5):
# def create_session_dir(output_dir): #TODO move to utils
# def create_subfolder(dir, name):
# def store_output_frames_as_frames(output_frames, labels, output_dir):
# def store_output_frames_as_gif(output_frames, labels, output_dir):
# def bgr_to_rgb(frame):
# def write_file_with_append(path, text_to_append):
# def write_metainfo(output_dir, model_name, flags):
# def store_dataframe(dataframe, output_dir, file_name):
# def store_latent_vectors_as_df(output_dir, hidden_representations, labels, metadata, video_file_paths=None, filename=None):
# def store_encoder_latent_vector(output_dir, hidden_representations, labels, produce_single_files=True):
# def store_plot(output_dir, name1, name2="", name3="", suffix=".png"):
# def export_plot_from_pickle(pickle_file_path, plot_options=((64, 64), 15, 15), show=False):
# def folder_names_from_dir(directory):
# def folder_files_dict(directory):
# def video_length(video_path):
# def get_class_mapping(mapping_document_path):
# def insert_general_classes_to_20bn_dataframe(df, mappings):
# def insert_general_class_to_20bn_dataframe(df, mapping_document_path, new_column_string="class_0", class_column="category"):
# def remove_rows_from_dataframe(df, to_remove, column="category"):
# def replace_char_from_dataframe(df, category, old_character, new_character):
# def select_subset_of_df_with_list(df, own_list, column="category"):
# def df_col_to_matrix(panda_col):
# def convert_frames_to_gif(frames_dir, gif_file_name=None, image_type='.png', fps=15, gif_file_path=None):
# def convert_gif_to_frames(gif_file_path):
# def frames_to_gif_in_dir_tree(root_dir):
# def gif_to_frames_in_dir_tree(root_dir):
# def generate_batch_from_dir(path, suffix="*.png"):
# def get_meta_info(filename, type=None, meta_dict = None):


import math, json, os.path, moviepy, os, multiprocessing, itertools
import random, multiprocessing, subprocess, scenedetect
import pandas as pd
import traceback
import shutil
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
from moviepy.editor import *
from utils import io_handler
from tensorflow.python.platform import gfile
from pprint import pprint
from joblib import Parallel, delayed

  target_video_name = generate_video_name(source_video_name, target_format, relative_crop_displacement, target_time_interval)
  target_video_path = os.path.join(output_dir, target_video_name)

  clip_resized = crop_and_resize_video_clip(source_video_path, video_file_clip=sub_clip, target_format=target_format,
                                       relative_crop_displacement=relative_crop_displacement)

  print("writing video: " + target_video_path)
  clip_resized.write_videofile(target_video_path, codec='rawvideo', progress_bar=False, verbose=False)
  kill_process(clip_resized)

def generate_video_name(source_video_name, target_format, relative_crop_displacement, time_interval):
  return source_video_name + '_' + str(target_format[0]) + 'x' + str(target_format[1]) + '_' \
                      + str("%.2f" % relative_crop_displacement) + '_' + "(%.1f,%.1f)" % time_interval\
                      + '.avi'

def get_metadata_dict(input_dir, output_dir, subclip_json_file_location, type):
  if type is 'json':
    # load dictionaries
    with open(subclip_json_file_location) as file:
      metadata_dict = json.load(file)

  elif type is 'folders':
    if os.path.isfile(os.path.join(output_dir, 'metadata.json')):
      with open(os.path.join(output_dir, 'metadata.json'), 'r')as f:
        metadata_dict = json.load(f)
      print('Loaded metadata dict from:', os.path.join(output_dir, 'metadata.json'))

    else:
      # generate metadata dict for videos

# Path: utils/io_handler.py
# def get_subdirectory_files(dir, depth=1):
# def files_from_directory(dir_str, file_type):
# def file_paths_from_directory(dir_str, file_type):
# def get_filename_and_filetype_from_path(path):
# def get_metadata_dict_as_bytes(value):
# def get_video_id_from_path(path_str, type=None):
# def shuffle_files_in_list(paths_list, seed=5):
# def shuffle_files_in_list_from_categories(paths_list, categories, metadata_path, type='youtube8m', seed=5):
# def create_session_dir(output_dir): #TODO move to utils
# def create_subfolder(dir, name):
# def store_output_frames_as_frames(output_frames, labels, output_dir):
# def store_output_frames_as_gif(output_frames, labels, output_dir):
# def bgr_to_rgb(frame):
# def write_file_with_append(path, text_to_append):
# def write_metainfo(output_dir, model_name, flags):
# def store_dataframe(dataframe, output_dir, file_name):
# def store_latent_vectors_as_df(output_dir, hidden_representations, labels, metadata, video_file_paths=None, filename=None):
# def store_encoder_latent_vector(output_dir, hidden_representations, labels, produce_single_files=True):
# def store_plot(output_dir, name1, name2="", name3="", suffix=".png"):
# def export_plot_from_pickle(pickle_file_path, plot_options=((64, 64), 15, 15), show=False):
# def folder_names_from_dir(directory):
# def folder_files_dict(directory):
# def video_length(video_path):
# def get_class_mapping(mapping_document_path):
# def insert_general_classes_to_20bn_dataframe(df, mappings):
# def insert_general_class_to_20bn_dataframe(df, mapping_document_path, new_column_string="class_0", class_column="category"):
# def remove_rows_from_dataframe(df, to_remove, column="category"):
# def replace_char_from_dataframe(df, category, old_character, new_character):
# def select_subset_of_df_with_list(df, own_list, column="category"):
# def df_col_to_matrix(panda_col):
# def convert_frames_to_gif(frames_dir, gif_file_name=None, image_type='.png', fps=15, gif_file_path=None):
# def convert_gif_to_frames(gif_file_path):
# def frames_to_gif_in_dir_tree(root_dir):
# def gif_to_frames_in_dir_tree(root_dir):
# def generate_batch_from_dir(path, suffix="*.png"):
# def get_meta_info(filename, type=None, meta_dict = None):


# Path: data_prep/video_preparation.py
import math, json, os.path, moviepy, os, multiprocessing, itertools
import random, multiprocessing, subprocess, scenedetect
import pandas as pd
import traceback
import shutil
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
from moviepy.editor import *
from utils import io_handler
from tensorflow.python.platform import gfile
from pprint import pprint
from joblib import Parallel, delayed


  target_video_name = generate_video_name(source_video_name, target_format, relative_crop_displacement, target_time_interval)
  target_video_path = os.path.join(output_dir, target_video_name)

  clip_resized = crop_and_resize_video_clip(source_video_path, video_file_clip=sub_clip, target_format=target_format,
                                       relative_crop_displacement=relative_crop_displacement)

  print("writing video: " + target_video_path)
  clip_resized.write_videofile(target_video_path, codec='rawvideo', progress_bar=False, verbose=False)
  kill_process(clip_resized)

def generate_video_name(source_video_name, target_format, relative_crop_displacement, time_interval):
  return source_video_name + '_' + str(target_format[0]) + 'x' + str(target_format[1]) + '_' \
                      + str("%.2f" % relative_crop_displacement) + '_' + "(%.1f,%.1f)" % time_interval\
                      + '.avi'

def get_metadata_dict(input_dir, output_dir, subclip_json_file_location, type):
  if type is 'json':
    # load dictionaries
    with open(subclip_json_file_location) as file:
      metadata_dict = json.load(file)

  elif type is 'folders':
    if os.path.isfile(os.path.join(output_dir, 'metadata.json')):
      with open(os.path.join(output_dir, 'metadata.json'), 'r')as f:
        metadata_dict = json.load(f)
      print('Loaded metadata dict from:', os.path.join(output_dir, 'metadata.json'))

    else:
      # generate metadata dict for videos

      ff_dict = io_handler.folder_files_dict(input_dir)