# 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
  return moviepy.video.fx.all.resize(clip_crop, newsize=target_format)

def get_activity_net_video_category(taxonomy_list, label):
  """requires the ucf101 taxonomy tree structure (from json file) and the clip label (e.g. 'Surfing)
  in order to find the corresponding category (e.g. 'Participating in water sports)''"""
  return search_list_of_dicts(taxonomy_list, 'nodeName', label)[0]['parentName']

def search_list_of_dicts(list_of_dicts, dict_key, dict_value):
  """parses through a list of dictionaries in order to return an entire dict entry that matches a given value (dict_value)
  for a given key (dict_key)"""
  return [entry for entry in list_of_dicts if entry[dict_key] == dict_value]

def get_metadata_dict_entry(subclip_dict_entry, taxonomy_list=None, type='activity_net'):
  """constructs and returns a dict entry consisting of the following entries (with UCF 101 example values):
  - label (Scuba diving)
  - category (Participating in water sports)
  - video_id (m_ST2LDe5lA_0)
  - filetype (mp4)
  - duration (12.627)
  - mode (training)
  - url (https://www.youtube.com/watch?v=m_ST2LDe5lA)"""
  meta_dict_entry = {}
  assert type in DATASETS
  assert taxonomy_list or not type=='activity_net'

  label = subclip_dict_entry['label']
  video_path = subclip_dict_entry['path']
  duration = subclip_dict_entry['duration']
  url = subclip_dict_entry['url']


# 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

  return moviepy.video.fx.all.resize(clip_crop, newsize=target_format)

def get_activity_net_video_category(taxonomy_list, label):
  """requires the ucf101 taxonomy tree structure (from json file) and the clip label (e.g. 'Surfing)
  in order to find the corresponding category (e.g. 'Participating in water sports)''"""
  return search_list_of_dicts(taxonomy_list, 'nodeName', label)[0]['parentName']

def search_list_of_dicts(list_of_dicts, dict_key, dict_value):
  """parses through a list of dictionaries in order to return an entire dict entry that matches a given value (dict_value)
  for a given key (dict_key)"""
  return [entry for entry in list_of_dicts if entry[dict_key] == dict_value]

def get_metadata_dict_entry(subclip_dict_entry, taxonomy_list=None, type='activity_net'):
  """constructs and returns a dict entry consisting of the following entries (with UCF 101 example values):
  - label (Scuba diving)
  - category (Participating in water sports)
  - video_id (m_ST2LDe5lA_0)
  - filetype (mp4)
  - duration (12.627)
  - mode (training)
  - url (https://www.youtube.com/watch?v=m_ST2LDe5lA)"""
  meta_dict_entry = {}
  assert type in DATASETS
  assert taxonomy_list or not type=='activity_net'

  label = subclip_dict_entry['label']
  video_path = subclip_dict_entry['path']
  duration = subclip_dict_entry['duration']
  url = subclip_dict_entry['url']


  video_id, filetype = io_handler.get_filename_and_filetype_from_path(video_path)