# import json
# import os
# import sox
# import uuid
#
# category_map = json.load(open("data/audioset/category.json"))
# url_data_list = json.load(open("data/audioset/urls.json"))
#
# src_aud_dir = "data/audioset/category/{}"
# tgt_aud_dir = "data/category/{}"
#
# for url_data in url_data_list:
#     rec_id = url_data["v_id"]
#     category = url_data["category"]
#
#     in_file = os.path.join(src_aud_dir.format(category), "{}.wav".format(rec_id))
#     if os.path.isfile(in_file):
#         start = float(url_data["start"])
#         end = float(url_data["end"])
#         tgt_aud_base_dir = tgt_aud_dir.format(category)
#         os.makedirs(tgt_aud_base_dir, exist_ok=True)
#         out_file = os.path.join(tgt_aud_base_dir, "{}.wav".format(str(uuid.uuid4())))
#         tfm = sox.Transformer()
#         tfm.trim(start, end)
#         tfm.set_output_format(file_type='wav', rate=16000, channels=1, bits=16, encoding='signed-integer')
#         status, out, err = tfm.build_file(input_filepath=in_file, output_filepath=out_file, return_output=True)
#
#         if status == 0:
#             print("Successfully moved '{}' to '{}'".format(in_file, out_file))


# TOOLS
# import csv
import json
from video_downloader import VideoExternalDownloader
import os

# src_csv_list = ["data/audioset/balanced_train_segments.csv",
#                 "data/audioset/eval_segments.csv",
#                 "data/audioset/unbalanced_train_segments.csv"]
#
# src_label_json = "data/audioset/ontology.json"
# tgt_url_list = "data/audioset/urls_full.json"
#
# tgt_aud = "data/category/{}"
# url_sig = "https://www.youtube.com/watch?v={}"
#
# count = 0
# with open(tgt_url_list, encoding="utf-8", mode="w") as tgt_url_json:
#     for src_csv in src_csv_list:
#         with open(src_csv) as csv_file:
#             csv_reader = csv.reader(csv_file, skipinitialspace=True)
#             next(csv_reader)
#             next(csv_reader)
#             next(csv_reader)
#             url_data_list = []
#             for row in csv_reader:
#                 v_id = str(row[0])
#                 start = float(row[1])
#                 end = float(row[2])
#                 labels = str(row[3]).split(",")
#
#                 if len(labels) <= 1 and labels[0] in category_map:
#                     label = labels[0]
#                     category = category_map[label]
#                     os.makedirs(tgt_aud.format(category), exist_ok=True)
#                     print("{} {} {} {} {}".format(v_id, start, end, label, category))
#                     url_data_list.append(
#                         {"v_id": v_id, "url": url_sig.format(v_id), "category": category, "start": start, "end": end})
#                     count = count + 1
#
#     json.dump(url_data_list, tgt_url_json)
#
#     print("\n\n COUNT: {}".format(count))

# with open(src_label_json) as ont_json:
#     ontology_list = json.load(ont_json)
#     for ontology in ontology_list:
#         print("{}: {}".format(ontology["id"], ontology["name"]))

# os.makedirs("data/audioset/category/test", exist_ok=True)

ved = VideoExternalDownloader()
with open("data/audioset/urls_ub.json") as url_json:
    url_data_list = json.load(url_json)
    for url_data in url_data_list:
        category = url_data["category"]
        if category not in ["music"]:
            print("{}: {}".format(url_data["url"], category))
            os.makedirs("data/audioset/category/{}".format(url_data["category"]), exist_ok=True)
            dwl_path = os.path.join("data/audioset/category/{}".format(url_data["category"]), '%(id)s.%(ext)s')
            try:
                ved.download(urls=[url_data["url"]], dwl_path=dwl_path)
            except Exception as e:
                print(str(e))

# import json
# import os
# import sox
# import uuid
#
# category_map = json.load(open("data/audioset/category.json"))
# url_data_list = json.load(open("data/audioset/urls.json"))
#
# src_aud_dir = "data/audioset/category/{}"
# tgt_aud_dir = "data/category/{}"
#
# for url_data in url_data_list:
#     rec_id = url_data["v_id"]
#     category = url_data["category"]
#
#     in_file = os.path.join(src_aud_dir.format(category), "{}.wav".format(rec_id))
#     if os.path.isfile(in_file):
#         start = float(url_data["start"])
#         end = float(url_data["end"])
#         tgt_aud_base_dir = tgt_aud_dir.format(category)
#         os.makedirs(tgt_aud_base_dir, exist_ok=True)
#         out_file = os.path.join(tgt_aud_base_dir, "{}.wav".format(str(uuid.uuid4())))
#         tfm = sox.Transformer()
#         tfm.trim(start, end)
#         tfm.set_output_format(file_type='wav', rate=16000, channels=1, bits=16, encoding='signed-integer')
#         status, out, err = tfm.build_file(input_filepath=in_file, output_filepath=out_file, return_output=True)
#
#         if status == 0:
#             print("Successfully moved '{}' to '{}'".format(in_file, out_file))


# TOOLS
# import csv
import json
from video_downloader import VideoExternalDownloader
import os

# src_csv_list = ["data/audioset/balanced_train_segments.csv",
#                 "data/audioset/eval_segments.csv",
#                 "data/audioset/unbalanced_train_segments.csv"]
#
# src_label_json = "data/audioset/ontology.json"
# tgt_url_list = "data/audioset/urls_full.json"
#
# tgt_aud = "data/category/{}"
# url_sig = "https://www.youtube.com/watch?v={}"
#
# count = 0
# with open(tgt_url_list, encoding="utf-8", mode="w") as tgt_url_json:
#     for src_csv in src_csv_list:
#         with open(src_csv) as csv_file:
#             csv_reader = csv.reader(csv_file, skipinitialspace=True)
#             next(csv_reader)
#             next(csv_reader)
#             next(csv_reader)
#             url_data_list = []
#             for row in csv_reader:
#                 v_id = str(row[0])
#                 start = float(row[1])
#                 end = float(row[2])
#                 labels = str(row[3]).split(",")
#
#                 if len(labels) <= 1 and labels[0] in category_map:
#                     label = labels[0]
#                     category = category_map[label]
#                     os.makedirs(tgt_aud.format(category), exist_ok=True)
#                     print("{} {} {} {} {}".format(v_id, start, end, label, category))
#                     url_data_list.append(
#                         {"v_id": v_id, "url": url_sig.format(v_id), "category": category, "start": start, "end": end})
#                     count = count + 1
#
#     json.dump(url_data_list, tgt_url_json)
#
#     print("\n\n COUNT: {}".format(count))

# with open(src_label_json) as ont_json:
#     ontology_list = json.load(ont_json)
#     for ontology in ontology_list:
#         print("{}: {}".format(ontology["id"], ontology["name"]))

# os.makedirs("data/audioset/category/test", exist_ok=True)

ved = VideoExternalDownloader()
with open("data/audioset/urls_ub.json") as url_json:
    url_data_list = json.load(url_json)
    for url_data in url_data_list:
        category = url_data["category"]
        if category not in ["music"]:
            print("{}: {}".format(url_data["url"], category))
            os.makedirs("data/audioset/category/{}".format(url_data["category"]), exist_ok=True)
            dwl_path = os.path.join("data/audioset/category/{}".format(url_data["category"]), '%(id)s.%(ext)s')
            try:
                ved.download(urls=[url_data["url"]], dwl_path=dwl_path)
            except Exception as e:
                print(str(e))

# import json # import os # import sox # import uuid # # category_map = json.load(open("data/audioset/category.json")) # url_data_list = json.load(open("data/audioset/urls.json")) # # src_aud_dir = "data/audioset/category/{}" # tgt_aud_dir = "data/category/{}" # # for url_data in url_data_list: #     rec_id = url_data["v_id"] #     category = url_data["category"] # #     in_file = os.path.join(src_aud_dir.format(category), "{}.wav".format(rec_id)) #     if os.path.isfile(in_file): #         start = float(url_data["start"]) #         end = float(url_data["end"]) #         tgt_aud_base_dir = tgt_aud_dir.format(category) #         os.makedirs(tgt_aud_base_dir, exist_ok=True) #         out_file = os.path.join(tgt_aud_base_dir, "{}.wav".format(str(uuid.uuid4()))) #         tfm = sox.Transformer() #         tfm.trim(start, end) #         tfm.set_output_format(file_type='wav', rate=16000, channels=1, bits=16, encoding='signed-integer') #         status, out, err = tfm.build_file(input_filepath=in_file, output_filepath=out_file, return_output=True) # #         if status == 0: #             print("Successfully moved '{}' to '{}'".format(in_file, out_file)) # TOOLS # import csv # src_csv_list = ["data/audioset/balanced_train_segments.csv", #                 "data/audioset/eval_segments.csv", #                 "data/audioset/unbalanced_train_segments.csv"] # # src_label_json = "data/audioset/ontology.json" # tgt_url_list = "data/audioset/urls_full.json" # # tgt_aud = "data/category/{}" # url_sig = "https://www.youtube.com/watch?v={}" # # count = 0 # with open(tgt_url_list, encoding="utf-8", mode="w") as tgt_url_json: #     for src_csv in src_csv_list: #         with open(src_csv) as csv_file: #             csv_reader = csv.reader(csv_file, skipinitialspace=True) #             next(csv_reader) #             next(csv_reader) #             next(csv_reader) #             url_data_list = [] #             for row in csv_reader: #                 v_id = str(row[0]) #                 start = float(row[1]) #                 end = float(row[2]) #                 labels = str(row[3]).split(",") # #                 if len(labels) <= 1 and labels[0] in category_map: #                     label = labels[0] #                     category = category_map[label] #                     os.makedirs(tgt_aud.format(category), exist_ok=True) #                     print("{} {} {} {} {}".format(v_id, start, end, label, category)) #                     url_data_list.append( #                         {"v_id": v_id, "url": url_sig.format(v_id), "category": category, "start": start, "end": end}) #                     count = count + 1 # #     json.dump(url_data_list, tgt_url_json) # #     print("\n\n COUNT: {}".format(count)) # with open(src_label_json) as ont_json: #     ontology_list = json.load(ont_json) #     for ontology in ontology_list: #         print("{}: {}".format(ontology["id"], ontology["name"])) # os.makedirs("data/audioset/category/test", exist_ok=True)