File size: 6,148 Bytes
ce4c55e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4.2.2-static\n"
]
}
],
"source": [
"import imageio_ffmpeg as ffmpeg\n",
"\n",
"print(ffmpeg.get_ffmpeg_version())"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"ffmpeg version 4.2.2-static https://johnvansickle.com/ffmpeg/ Copyright (c) 2000-2019 the FFmpeg developers\n",
" built with gcc 8 (Debian 8.3.0-6)\n",
" configuration: --enable-gpl --enable-version3 --enable-static --disable-debug --disable-ffplay --disable-indev=sndio --disable-outdev=sndio --cc=gcc --enable-fontconfig --enable-frei0r --enable-gnutls --enable-gmp --enable-libgme --enable-gray --enable-libaom --enable-libfribidi --enable-libass --enable-libvmaf --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librubberband --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libvorbis --enable-libopus --enable-libtheora --enable-libvidstab --enable-libvo-amrwbenc --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libdav1d --enable-libxvid --enable-libzvbi --enable-libzimg\n",
" libavutil 56. 31.100 / 56. 31.100\n",
" libavcodec 58. 54.100 / 58. 54.100\n",
" libavformat 58. 29.100 / 58. 29.100\n",
" libavdevice 58. 8.100 / 58. 8.100\n",
" libavfilter 7. 57.100 / 7. 57.100\n",
" libswscale 5. 5.100 / 5. 5.100\n",
" libswresample 3. 5.100 / 3. 5.100\n",
" libpostproc 55. 5.100 / 55. 5.100\n",
"Guessed Channel Layout for Input Stream #0.0 : mono\n",
"Input #0, wav, from '_EHyrPDKsvA.wav':\n",
" Metadata:\n",
" encoder : Lavf58.29.100\n",
" Duration: 00:03:58.41, bitrate: 256 kb/s\n",
" Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 16000 Hz, mono, s16, 256 kb/s\n",
"Stream mapping:\n",
" Stream #0:0 -> #0:0 (pcm_s16le (native) -> adpcm_ms (native))\n",
"Press [q] to stop, [?] for help\n",
"Output #0, wav, to 'compressed_EHyrPDKsvA.wav':\n",
" Metadata:\n",
" ISFT : Lavf58.29.100\n",
" Stream #0:0: Audio: adpcm_ms ([2][0][0][0] / 0x0002), 16000 Hz, mono, s16, 64 kb/s\n",
" Metadata:\n",
" encoder : Lavc58.54.100 adpcm_ms\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Compressed file saved to: compressed_EHyrPDKsvA.wav\n",
"File: compressed_EHyrPDKsvA.wav\n",
"Sample rate: 16000\n",
"Channels: 1\n",
"Frames: 3815464\n",
"Duration: 238.47 seconds\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"size= 1874kB time=00:03:58.46 bitrate= 64.4kbits/s speed=3.21e+03x \n",
"video:0kB audio:1874kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.006462%\n"
]
}
],
"source": [
"import subprocess\n",
"import soundfile as sf\n",
"import imageio_ffmpeg as ffmpeg\n",
"\n",
"\n",
"def compress_wav(input_file, output_file):\n",
" \"\"\"\n",
" Compress a WAV file using ADPCM codec with ffmpeg from imageio_ffmpeg.\n",
"\n",
" Args:\n",
" input_file (str): Path to the original WAV file.\n",
" output_file (str): Path to save the compressed WAV file.\n",
" \"\"\"\n",
" # Get the ffmpeg executable path\n",
" ffmpeg_path = ffmpeg.get_ffmpeg_exe()\n",
"\n",
" try:\n",
" # Run the ffmpeg command to compress the file\n",
" subprocess.run(\n",
" [\n",
" ffmpeg_path,\n",
" \"-i\",\n",
" input_file, # Input file\n",
" \"-c:a\",\n",
" \"adpcm_ms\", # Use ADPCM codec\n",
" output_file, # Output file\n",
" ],\n",
" check=True,\n",
" )\n",
" print(f\"Compressed file saved to: {output_file}\")\n",
" except subprocess.CalledProcessError as e:\n",
" print(f\"Error during compression: {e}\")\n",
"\n",
"\n",
"def read_wav(file_path):\n",
" \"\"\"\n",
" Read and display metadata of a WAV file using soundfile.\n",
"\n",
" Args:\n",
" file_path (str): Path to the WAV file.\n",
" \"\"\"\n",
" try:\n",
" with sf.SoundFile(file_path) as f:\n",
" print(f\"File: {file_path}\")\n",
" print(f\"Sample rate: {f.samplerate}\")\n",
" print(f\"Channels: {f.channels}\")\n",
" print(f\"Frames: {f.frames}\")\n",
" print(f\"Duration: {f.frames / f.samplerate:.2f} seconds\")\n",
" except Exception as e:\n",
" print(f\"Error reading file: {e}\")\n",
"\n",
"\n",
"# Paths for the input and output files\n",
"input_wav = \"_EHyrPDKsvA.wav\" # Replace with your input WAV file path\n",
"compressed_wav = \"compressed_EHyrPDKsvA.wav\"\n",
"\n",
"# Compress the WAV file\n",
"compress_wav(input_wav, compressed_wav)\n",
"\n",
"# Read the compressed WAV file metadata\n",
"read_wav(compressed_wav)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|