{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport subprocess\nimport glob\nimport os\nfrom pathlib import Path\nimport shutil\nfrom zipfile import ZipFile\nfrom scipy import signal\nfrom scipy.io import wavfile\nfrom skopt import gp_minimize\nfrom skopt.space import Real\nfrom functools import partial\nimport librosa.display\nimport librosa.filters\nimport matplotlib.pyplot as plt\nimport skimage","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-01T13:00:56.756897Z","iopub.execute_input":"2023-04-01T13:00:56.757285Z","iopub.status.idle":"2023-04-01T13:01:00.359010Z","shell.execute_reply.started":"2023-04-01T13:00:56.757224Z","shell.execute_reply":"2023-04-01T13:01:00.357372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Using the Static Build of ffmpeg from https://johnvansickle.com/ffmpeg/ because internet is not available. <br>\nThe public data set can be found here:\nhttps://www.kaggle.com/rakibilly/ffmpeg-static-build\n","metadata":{}},{"cell_type":"code","source":"! tar xvf ../input/ffmpeg-static-build/ffmpeg-git-amd64-static.tar.xz","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-04-01T13:01:00.361654Z","iopub.execute_input":"2023-04-01T13:01:00.361984Z","iopub.status.idle":"2023-04-01T13:01:06.715922Z","shell.execute_reply.started":"2023-04-01T13:01:00.361924Z","shell.execute_reply":"2023-04-01T13:01:06.714900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Specify output format and create a directory for the output Audio files\nFor 400 mp3 files, the directory is approx 94 MB.<br>\nFor 400 wav files, the directory is approx 673 MB.","metadata":{}},{"cell_type":"code","source":"json_metadata_path = '/kaggle/input/realfake045/metadata.json'\ndata_df = pd.read_json(json_metadata_path)\ndata_df = data_df.T","metadata":{"execution":{"iopub.status.busy":"2023-04-01T13:01:06.718033Z","iopub.execute_input":"2023-04-01T13:01:06.718346Z","iopub.status.idle":"2023-04-01T13:01:08.128016Z","shell.execute_reply.started":"2023-04-01T13:01:06.718284Z","shell.execute_reply":"2023-04-01T13:01:08.126745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_df.head()\ndata_dict = {}\nreals = set()\nfor index, row in data_df.iterrows():\n    fake_name = index.split(\".\")[0]\n    if row.label == \"FAKE\":\n        real_name = row.original.split(\".\")[0]\n        data_dict[fake_name] = real_name\n        reals.add(real_name)","metadata":{"execution":{"iopub.status.busy":"2023-04-01T13:01:08.129989Z","iopub.execute_input":"2023-04-01T13:01:08.130397Z","iopub.status.idle":"2023-04-01T13:01:08.482458Z","shell.execute_reply.started":"2023-04-01T13:01:08.130315Z","shell.execute_reply":"2023-04-01T13:01:08.481460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list_of_files = []\nfile_dict = {}\nfor file in os.listdir(\"/kaggle/input/realfake045/all/all\"):\n    name = file.split('.')[0]\n    if name in data_dict:\n        list_of_files.append(\"/kaggle/input/realfake045/all/all/\"+name+\".mp4\")\n        real_ = data_dict[name]\n        if real_ in reals:\n            list_of_files.append(\"/kaggle/input/realfake045/all/all/\"+real_+\".mp4\")\n            file_dict[name] = real_\nprint(len(list_of_files))","metadata":{"execution":{"iopub.status.busy":"2023-04-01T13:01:08.483940Z","iopub.execute_input":"2023-04-01T13:01:08.484353Z","iopub.status.idle":"2023-04-01T13:01:08.496301Z","shell.execute_reply.started":"2023-04-01T13:01:08.484267Z","shell.execute_reply":"2023-04-01T13:01:08.495377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir(\"./wavs\")","metadata":{"execution":{"iopub.status.busy":"2023-04-01T13:01:26.587529Z","iopub.execute_input":"2023-04-01T13:01:26.587877Z","iopub.status.idle":"2023-04-01T13:01:26.592595Z","shell.execute_reply.started":"2023-04-01T13:01:26.587834Z","shell.execute_reply":"2023-04-01T13:01:26.591547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output_dir = \"/kaggle/working/wavs\"\noutput_format = \"wav\"","metadata":{"execution":{"iopub.status.busy":"2023-04-01T13:01:28.697033Z","iopub.execute_input":"2023-04-01T13:01:28.697612Z","iopub.status.idle":"2023-04-01T13:01:28.702316Z","shell.execute_reply.started":"2023-04-01T13:01:28.697532Z","shell.execute_reply":"2023-04-01T13:01:28.701620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Extract the audio from files","metadata":{}},{"cell_type":"code","source":"def create_wav(list_of_files):\n    for file in list_of_files:\n        name = file.split(\"/\")[-1].split('.')[0]\n        command = f\"../working/ffmpeg-git-20191209-amd64-static/ffmpeg -i {file} -ab 192000 -ac 2 -ar 44100 -vn {output_dir}/{name}.{output_format}\"\n        subprocess.call(command, shell=True)","metadata":{"execution":{"iopub.status.busy":"2023-04-01T13:01:30.821676Z","iopub.execute_input":"2023-04-01T13:01:30.822250Z","iopub.status.idle":"2023-04-01T13:01:30.827123Z","shell.execute_reply.started":"2023-04-01T13:01:30.822176Z","shell.execute_reply":"2023-04-01T13:01:30.826460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ncreate_wav(list_of_files)","metadata":{"execution":{"iopub.status.busy":"2023-04-01T13:01:31.927120Z","iopub.execute_input":"2023-04-01T13:01:31.927737Z","iopub.status.idle":"2023-04-01T13:02:29.434099Z","shell.execute_reply.started":"2023-04-01T13:01:31.927669Z","shell.execute_reply":"2023-04-01T13:02:29.432633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_spectogram(name,sr):\n    WAV_PATH = \"/kaggle/working/wavs/\"\n    audio_array, sample_rate = librosa.load(WAV_PATH+f'{name}', sr=sr)\n    trim_audio_array, index = librosa.effects.trim(audio_array)\n    S = librosa.feature.melspectrogram(y=trim_audio_array, sr=sr, n_mels=128, fmax=8000)\n    S_dB = np.log(S + 1e-9)\n    # min-max scale to fit inside 8-bit range\n    img = scale_minmax(S_dB, 0, 255).astype(np.uint8)\n    img = np.flip(img, axis=0) # put low frequencies at the bottom in image\n    img = 255-img # invert. make black==more energy\n    #S_dB = librosa.power_to_db(S, ref=np.max)\n    return S_dB ,img\n\ndef scale_minmax(X, min=0.0, max=1.0):\n    X_std = (X - X.min()) / (X.max() - X.min())\n    X_scaled = X_std * (max - min) + min\n    return X_scaled","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-04-01T13:02:29.436815Z","iopub.execute_input":"2023-04-01T13:02:29.437159Z","iopub.status.idle":"2023-04-01T13:02:29.447522Z","shell.execute_reply.started":"2023-04-01T13:02:29.437106Z","shell.execute_reply":"2023-04-01T13:02:29.446466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ni=0\nsr=20000\nfake_images = []\nreal_images = []\nerror = 0\nfor fake_name, real_name in file_dict.items():\n    f_name = fake_name +'.wav'\n    r_name = real_name +'.wav'\n    path = \"/kaggle/working/wavs/\"\n    try:\n        S_fake,img_fake =create_spectogram(f_name,sr)\n        S_real,img_real =create_spectogram(r_name,sr)\n#         if not(np.array_equal(S_fake,S_real)):\n#             diff = np.sum(np.abs(S_real - S_fake))\n#             print(f\"There is a difference in Audio : {diff}\")\n#             plt.figure(figsize=(10, 4))\n#             plt.axis('off')\n#                             #librosa.display.specshow(S_fake, x_axis='time',\n#                             #          y_axis='mel', sr=sr,\n#                             #          fmax=8000)\n#             plt.imshow(img_fake,cmap='gray')\n#             plt.colorbar(format='%+2.0f dB')\n#             plt.title(f'Mel-frequency spectrogram Fake name {fake_name}')\n#             plt.tight_layout()\n#             plt.show()\n#             plt.figure(figsize=(10, 4))\n#             plt.axis('off')\n#                             #librosa.display.specshow(S_real, x_axis='time',\n#                             #          y_axis='mel', sr=sr,\n#                             #          fmax=8000)\n#             plt.imshow(img_real,cmap='gray')\n#             plt.colorbar(format='%+2.0f dB')\n#             plt.title(f'Mel-frequency spectrogram Real name {real_name}')\n#             plt.tight_layout()\n#             plt.show()\n        fake_images.append(img_fake)\n        real_images.append(img_real)\n    except:\n        error+=1\nprint(str(error)+\" failed out of \"+ str(len(file_dict)))","metadata":{"execution":{"iopub.status.busy":"2023-04-01T13:02:29.449070Z","iopub.execute_input":"2023-04-01T13:02:29.449452Z","iopub.status.idle":"2023-04-01T13:11:14.030549Z","shell.execute_reply.started":"2023-04-01T13:02:29.449325Z","shell.execute_reply":"2023-04-01T13:11:14.028735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(fake_images), len(real_images))","metadata":{"execution":{"iopub.status.busy":"2023-04-01T13:11:14.033875Z","iopub.execute_input":"2023-04-01T13:11:14.034648Z","iopub.status.idle":"2023-04-01T13:11:14.045014Z","shell.execute_reply.started":"2023-04-01T13:11:14.034407Z","shell.execute_reply":"2023-04-01T13:11:14.043403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir(\"./real_audio_images\")\nos.mkdir(\"./fake_audio_images\")","metadata":{"execution":{"iopub.status.busy":"2023-04-01T13:14:06.609140Z","iopub.execute_input":"2023-04-01T13:14:06.609578Z","iopub.status.idle":"2023-04-01T13:14:06.615256Z","shell.execute_reply.started":"2023-04-01T13:14:06.609517Z","shell.execute_reply":"2023-04-01T13:14:06.614100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = 1\nfor image in fake_images:\n    fig = plt.figure()\n    ax = plt.Axes(fig, [0., 0., 1., 1.])\n    ax.set_axis_off()\n    fig.add_axes(ax)\n    plt.set_cmap('gray')\n    ax.imshow(image, aspect='equal')\n    plt.savefig(\"/kaggle/working/fake_audio_images/\"+str(i)+\".png\")\n    plt.close()\n    i+=1\ni = 1\nfor image in real_images:\n    fig = plt.figure()\n    ax = plt.Axes(fig, [0., 0., 1., 1.])\n    ax.set_axis_off()\n    fig.add_axes(ax)\n    plt.set_cmap('gray')\n    ax.imshow(image, aspect='equal')\n    plt.savefig(\"/kaggle/working/real_audio_images/\"+str(i)+\".png\")\n    plt.close()\n    i+=1\n","metadata":{"execution":{"iopub.status.busy":"2023-04-01T13:24:16.847721Z","iopub.execute_input":"2023-04-01T13:24:16.848247Z","iopub.status.idle":"2023-04-01T13:25:16.933455Z","shell.execute_reply.started":"2023-04-01T13:24:16.848189Z","shell.execute_reply":"2023-04-01T13:25:16.932658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Create ZIP file","metadata":{}},{"cell_type":"code","source":"with ZipFile('./audio_images.zip', 'w') as zipObj:\n   # Iterate over all the files in directory\n    for folderName, subfolders, filenames in os.walk(\"/kaggle/working/real_audio_images\"):\n        for filename in filenames:\n           #create complete filepath of file in directory\n            filePath = os.path.join(folderName, filename)\n           # Add file to zip\n            zipObj.write(filePath)\n    for folderName, subfolders, filenames in os.walk(\"/kaggle/working/fake_audio_images\"):\n        for filename in filenames:\n           #create complete filepath of file in directory\n            filePath = os.path.join(folderName, filename)\n           # Add file to zip\n            zipObj.write(filePath)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-01T13:27:28.658139Z","iopub.execute_input":"2023-04-01T13:27:28.658558Z","iopub.status.idle":"2023-04-01T13:27:29.073483Z","shell.execute_reply.started":"2023-04-01T13:27:28.658501Z","shell.execute_reply":"2023-04-01T13:27:29.072287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Cleanup","metadata":{}},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}