{"cells":[{"metadata":{},"cell_type":"markdown","source":"# This notebook enables the extraction of audio from the video files without internet.\n\nIf you find it useful, please upvote. Any suggestions to improve the code are welcome.\n"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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","execution_count":null,"outputs":[]},{"metadata":{},"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":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"! tar xvf ../input/ffmpeg-static-build/ffmpeg-git-amd64-static.tar.xz","execution_count":null,"outputs":[]},{"metadata":{},"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":{"trusted":true},"cell_type":"code","source":"output_format = 'mp3'  # can also use aac, wav, etc\n\noutput_dir = Path(f\"{output_format}s\")\nPath(output_dir).mkdir(exist_ok=True, parents=True)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Get the list of videos to extract audio from"},{"metadata":{"trusted":true},"cell_type":"code","source":"list_of_files = glob.glob('../input/deepfake-detection-challenge/train_sample_videos/*.mp4') ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Extract the audio from files"},{"metadata":{"trusted":true},"cell_type":"code","source":"for file in list_of_files:\n    command = f\"../working/ffmpeg-git-20191209-amd64-static/ffmpeg -i {file} -ab 192000 -ac 2 -ar 44100 -vn {output_dir/file[-14:-4]}.{output_format}\"\n    subprocess.call(command, shell=True)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Create ZIP file"},{"metadata":{"trusted":true},"cell_type":"code","source":"with ZipFile(f'all_{output_format}s.zip', 'w') as zipObj:\n   # Iterate over all the files in directory\n   for folderName, subfolders, filenames in os.walk(f'./{output_format}s/'):\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)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"#### Cleanup"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Remove FFMPEG directory from output\nshutil.rmtree(\"../working/ffmpeg-git-20191209-amd64-static\")\n# Remove directory of output files\nshutil.rmtree(f'./{output_format}s/')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.listdir()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}