Skip to content

Instantly share code, notes, and snippets.

@asus4
Created March 18, 2020 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asus4/f604e621a1aedde50091101e11b037bd to your computer and use it in GitHub Desktop.
Save asus4/f604e621a1aedde50091101e11b037bd to your computer and use it in GitHub Desktop.
CheckAudioLoudness.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "CheckAudioLoudness.ipynb",
"provenance": [],
"toc_visible": true,
"authorship_tag": "ABX9TyNyA+L2k4KcQfNMBCbWmTD2",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/asus4/f604e621a1aedde50091101e11b037bd/checkaudioloudness.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "g94PM2huHcx7",
"colab_type": "code",
"outputId": "273007ed-1e43-4750-c721-6f99f80d42a4",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"!which ffmpeg"
],
"execution_count": 28,
"outputs": [
{
"output_type": "stream",
"text": [
"/usr/bin/ffmpeg\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "KzYNvK_CHpp0",
"colab_type": "code",
"outputId": "a0907ecc-1d54-452c-841b-3875d24a906f",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 124
}
},
"source": [
"import os\n",
"\n",
"mount_point = '/content/gdrive' #@param {type:'string'}\n",
"\n",
"if os.path.isdir(mount_point):\n",
" print(mount_point + ' has been already mounted.')\n",
"else:\n",
" from google.colab import drive\n",
" drive.mount(mount_point)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3aietf%3awg%3aoauth%3a2.0%3aoob&response_type=code&scope=email%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdocs.test%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive.photos.readonly%20https%3a%2f%2fwww.googleapis.com%2fauth%2fpeopleapi.readonly\n",
"\n",
"Enter your authorization code:\n",
"··········\n",
"Mounted at /content/gdrive\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "Gdd3HK_JIwm2",
"colab_type": "code",
"outputId": "30362d7f-1242-495a-980b-6c47cd970e9d",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"#@markdown The input folder which includes mp3 files\n",
"\n",
"\n",
"import glob\n",
"\n",
"input_dir = '/My Drive/datasets/mp3s' #@param {type:\"string\"}\n",
"input_dir = mount_point + input_dir\n",
"\n",
"sound_paths = glob.glob(input_dir + '/**/*.mp3')\n",
"\n",
"print('Found {0} files'.format(len(sound_paths)))\n",
"# print(sound_paths)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"Found 21 files\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "NWZz-QqdJ_-6",
"colab_type": "code",
"outputId": "532ddfcc-9aa5-43c5-863a-ddd9161f20af",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 1000
}
},
"source": [
"import shlex\n",
"import subprocess\n",
"\n",
"# https://developers.google.com/assistant/tools/audio-loudness#using_a_daw_and_lufs_meter\n",
"\n",
"def output_filter(line):\n",
" topics = ['Input Integrated:',\n",
" 'Input True Peak:',\n",
" 'Input LRA:',\n",
" 'Input Threshold:',\n",
" 'Target Offset:']\n",
" for topic in topics:\n",
" if line.startswith(topic):\n",
" return True\n",
" return False\n",
"\n",
"def check_loudness(input_file):\n",
" command = 'ffmpeg -i \"{0}\" -af loudnorm=I=-16:dual_mono=true:TP=-1.5:LRA=11:print_format=summary -f null -'.format(input_file)\n",
" args = shlex.split(command)\n",
" process = subprocess.Popen(args,\n",
" stdout=subprocess.PIPE, \n",
" stderr=subprocess.STDOUT,\n",
" universal_newlines=True)\n",
" stdout, stderr = process.communicate()\n",
" # print(stdout)\n",
" lines = stdout.splitlines()\n",
" lines = filter(output_filter, lines)\n",
" for line in lines:\n",
" print(line)\n",
"\n",
"for path in sound_paths:\n",
" print('\\n==========================')\n",
" print('File: {0}\\n'.format(os.path.basename(path)))\n",
" check_loudness(path)\n"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"\n",
"==========================\n",
"File: MichelJackson_BlackOrWhite.mp3\n",
"\n",
"Input Integrated: -23.1 LUFS\n",
"Input True Peak: -10.2 dBTP\n",
"Input LRA: 3.8 LU\n",
"Input Threshold: -33.2 LUFS\n",
"Target Offset: -0.8 LU\n",
"\n",
"==========================\n",
"File: Madeon_AllMyFriends.mp3\n",
"\n",
"Input Integrated: -23.1 LUFS\n",
"Input True Peak: -14.1 dBTP\n",
"Input LRA: 4.9 LU\n",
"Input Threshold: -33.2 LUFS\n",
"Target Offset: -1.2 LU\n",
"\n",
"==========================\n",
"File: LedZeppelin_ImmigrantSong.mp3\n",
"\n",
"Input Integrated: -23.1 LUFS\n",
"Input True Peak: -11.6 dBTP\n",
"Input LRA: 2.9 LU\n",
"Input Threshold: -33.3 LUFS\n",
"Target Offset: -0.5 LU\n",
"\n",
"==========================\n",
"File: Coldplay_VivaLaVida.mp3\n",
"\n",
"Input Integrated: -23.1 LUFS\n",
"Input True Peak: -13.2 dBTP\n",
"Input LRA: 6.9 LU\n",
"Input Threshold: -33.4 LUFS\n",
"Target Offset: -1.4 LU\n",
"\n",
"==========================\n",
"File: VanHalen_Jump.mp3\n",
"\n",
"Input Integrated: -23.2 LUFS\n",
"Input True Peak: -11.5 dBTP\n",
"Input LRA: 5.0 LU\n",
"Input Threshold: -33.4 LUFS\n",
"Target Offset: -0.5 LU\n",
"\n",
"==========================\n",
"File: TaylorSwift_WeAreNeverEverGettingBackTogether.mp3\n",
"\n",
"Input Integrated: -23.1 LUFS\n",
"Input True Peak: -12.9 dBTP\n",
"Input LRA: 7.9 LU\n",
"Input Threshold: -33.4 LUFS\n",
"Target Offset: -1.4 LU\n",
"\n",
"==========================\n",
"File: Sting_EnglishmanInNewYork.mp3\n",
"\n",
"Input Integrated: -23.1 LUFS\n",
"Input True Peak: -7.2 dBTP\n",
"Input LRA: 6.5 LU\n",
"Input Threshold: -33.3 LUFS\n",
"Target Offset: -0.8 LU\n",
"\n",
"==========================\n",
"File: StevieWonder_SirDuke.mp3\n",
"\n",
"Input Integrated: -23.1 LUFS\n",
"Input True Peak: -9.7 dBTP\n",
"Input LRA: 5.5 LU\n",
"Input Threshold: -33.2 LUFS\n",
"Target Offset: -1.4 LU\n",
"\n",
"==========================\n",
"File: Queen_DontStopMeNow.mp3\n",
"\n",
"Input Integrated: -23.1 LUFS\n",
"Input True Peak: -12.8 dBTP\n",
"Input LRA: 8.5 LU\n",
"Input Threshold: -33.4 LUFS\n",
"Target Offset: -1.1 LU\n",
"\n",
"==========================\n",
"File: Prince_LetsGoCrazy.mp3\n",
"\n",
"Input Integrated: -23.1 LUFS\n",
"Input True Peak: -13.2 dBTP\n",
"Input LRA: 4.1 LU\n",
"Input Threshold: -33.2 LUFS\n",
"Target Offset: -0.9 LU\n",
"\n",
"==========================\n",
"File: OwlCity_GoodTime.mp3\n",
"\n",
"Input Integrated: -23.1 LUFS\n",
"Input True Peak: -13.9 dBTP\n",
"Input LRA: 6.4 LU\n",
"Input Threshold: -33.4 LUFS\n",
"Target Offset: -1.7 LU\n",
"\n",
"==========================\n",
"File: JohannPachelbel_CanonInDMajor.mp3\n",
"\n",
"Input Integrated: -23.1 LUFS\n",
"Input True Peak: -10.9 dBTP\n",
"Input LRA: 5.1 LU\n",
"Input Threshold: -33.1 LUFS\n",
"Target Offset: -1.2 LU\n",
"\n",
"==========================\n",
"File: JacoPastrius_ComeOnComeOver.mp3\n",
"\n",
"Input Integrated: -23.1 LUFS\n",
"Input True Peak: -8.8 dBTP\n",
"Input LRA: 4.0 LU\n",
"Input Threshold: -33.2 LUFS\n",
"Target Offset: -0.3 LU\n",
"\n",
"==========================\n",
"File: IkuyoNakamichi_Fantasy.mp3\n",
"\n",
"Input Integrated: -23.0 LUFS\n",
"Input True Peak: -6.3 dBTP\n",
"Input LRA: 15.2 LU\n",
"Input Threshold: -33.9 LUFS\n",
"Target Offset: -0.3 LU\n",
"\n",
"==========================\n",
"File: FreddieKing_SweetHomeChicago.mp3\n",
"\n",
"Input Integrated: -23.1 LUFS\n",
"Input True Peak: -8.2 dBTP\n",
"Input LRA: 4.9 LU\n",
"Input Threshold: -33.1 LUFS\n",
"Target Offset: -0.7 LU\n",
"\n",
"==========================\n",
"File: Eagles_TakeItEasy.mp3\n",
"\n",
"Input Integrated: -23.1 LUFS\n",
"Input True Peak: -10.9 dBTP\n",
"Input LRA: 4.4 LU\n",
"Input Threshold: -33.2 LUFS\n",
"Target Offset: -0.9 LU\n",
"\n",
"==========================\n",
"File: BillEvans_WaltzForDebby.mp3\n",
"\n",
"Input Integrated: -23.1 LUFS\n",
"Input True Peak: -5.9 dBTP\n",
"Input LRA: 6.2 LU\n",
"Input Threshold: -33.2 LUFS\n",
"Target Offset: -0.3 LU\n",
"\n",
"==========================\n",
"File: BennyGoodman_SingSingSing.mp3\n",
"\n",
"Input Integrated: -23.1 LUFS\n",
"Input True Peak: -8.6 dBTP\n",
"Input LRA: 8.7 LU\n",
"Input Threshold: -33.2 LUFS\n",
"Target Offset: -0.4 LU\n",
"\n",
"==========================\n",
"File: WilhelmKempff_Moonlight.mp3\n",
"\n",
"Input Integrated: -23.0 LUFS\n",
"Input True Peak: -4.6 dBTP\n",
"Input LRA: 11.4 LU\n",
"Input Threshold: -33.5 LUFS\n",
"Target Offset: +0.1 LU\n",
"\n",
"==========================\n",
"File: LudwigvanBeethoven_Pastoral.mp3\n",
"\n",
"Input Integrated: -23.0 LUFS\n",
"Input True Peak: -6.2 dBTP\n",
"Input LRA: 21.3 LU\n",
"Input Threshold: -34.9 LUFS\n",
"Target Offset: -0.4 LU\n",
"\n",
"==========================\n",
"File: JohnColtrane_MyFavoriteThings.mp3\n",
"\n",
"Input Integrated: -23.1 LUFS\n",
"Input True Peak: -7.8 dBTP\n",
"Input LRA: 5.0 LU\n",
"Input Threshold: -33.2 LUFS\n",
"Target Offset: -0.4 LU\n"
],
"name": "stdout"
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment