Last active
February 23, 2026 06:55
-
-
Save shhommychon/e9a830319c2b3746c98c7f0340b80e2d to your computer and use it in GitHub Desktop.
내 블로그에 쓰려고 만든 mp3 포맷 변경 코드
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "nbformat": 4, | |
| "nbformat_minor": 0, | |
| "metadata": { | |
| "colab": { | |
| "provenance": [], | |
| "collapsed_sections": [ | |
| "EmwfiTd-1I9P", | |
| "p9-eKesT0uzf" | |
| ] | |
| }, | |
| "kernelspec": { | |
| "name": "python3", | |
| "display_name": "Python 3" | |
| }, | |
| "language_info": { | |
| "name": "python" | |
| } | |
| }, | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "[](https://colab.research.google.com/gist/shhommychon/e9a830319c2b3746c98c7f0340b80e2d/mp3_to_pkl.ipynb)" | |
| ], | |
| "metadata": { | |
| "id": "wcKjJopq4312" | |
| } | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "##### misc" | |
| ], | |
| "metadata": { | |
| "id": "EmwfiTd-1I9P" | |
| } | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "###### setup" | |
| ], | |
| "metadata": { | |
| "id": "p9-eKesT0uzf" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "!pip install -qq mutagen==1.47.0" | |
| ], | |
| "metadata": { | |
| "id": "0xLdSV2w0wHs", | |
| "executionInfo": { | |
| "status": "ok", | |
| "timestamp": 1753235512769, | |
| "user_tz": -540, | |
| "elapsed": 8107 | |
| } | |
| }, | |
| "execution_count": 1, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "###### code" | |
| ], | |
| "metadata": { | |
| "id": "XSgUbWtd1D6S" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "import os\n", | |
| "import shutil\n", | |
| "import zipfile\n", | |
| "\n", | |
| "from mutagen.mp3 import MP3\n", | |
| "import pickle" | |
| ], | |
| "metadata": { | |
| "id": "aOtLjDEc07ip", | |
| "executionInfo": { | |
| "status": "ok", | |
| "timestamp": 1753235512790, | |
| "user_tz": -540, | |
| "elapsed": 19 | |
| } | |
| }, | |
| "execution_count": 2, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "class MyPickler():\n", | |
| " temp_mp3 = \"temp_stripped.mp3\"\n", | |
| " temp_zip = \"temp_archive.zip\"\n", | |
| " arcname = \"audio.mp3\"\n", | |
| "\n", | |
| " def mp3_to_my_pkl(self, input_mp3, output_pkl):\n", | |
| " \"\"\"MP3 메타데이터 삭제 -> ZIP 압축 -> 순수 바이너리를 pkl로 저장합니다.\"\"\"\n", | |
| "\n", | |
| " # 1. 원본 훼손을 막기 위해 임시 파일로 작업\n", | |
| " shutil.copyfile(input_mp3, self.temp_mp3)\n", | |
| "\n", | |
| " # 2. 내부 메타데이터(ID3 태그) 제거\n", | |
| " try:\n", | |
| " audio = MP3(self.temp_mp3)\n", | |
| " audio.delete()\n", | |
| " audio.save()\n", | |
| " except Exception:\n", | |
| " pass\n", | |
| "\n", | |
| " # 3. 메타데이터가 제거된 MP3를 ZIP으로 압축\n", | |
| " # ZIP_DEFLATED 알고리즘을 사용하여 압축을 시도합니다.\n", | |
| " with zipfile.ZipFile(self.temp_zip, 'w', zipfile.ZIP_DEFLATED) as zipf:\n", | |
| " # 압축 파일 내부에서 사용할 파일명 지정\n", | |
| " zipf.write(self.temp_mp3, arcname=self.arcname)\n", | |
| "\n", | |
| " # 4. ZIP 파일의 바이너리 데이터 읽기\n", | |
| " with open(self.temp_zip, \"rb\") as f:\n", | |
| " zip_binary = f.read()\n", | |
| "\n", | |
| " # 5. 바이너리 데이터를 pkl로 직렬화\n", | |
| " with open(output_pkl, \"wb\") as f:\n", | |
| " pickle.dump(zip_binary, f)\n", | |
| "\n", | |
| " # 작업에 사용된 임시 파일 삭제\n", | |
| " os.remove(self.temp_mp3)\n", | |
| " os.remove(self.temp_zip)\n", | |
| "\n", | |
| " print(f\"직렬화 완료: {input_mp3} -> {output_pkl}\")\n", | |
| "\n", | |
| " def my_pkl_to_mp3(self, input_pkl, output_mp3):\n", | |
| " \"\"\"pkl 역직렬화 -> ZIP 파일 임시 생성 -> 압축 해제 후 mp3로 복원합니다.\"\"\"\n", | |
| "\n", | |
| " # 1. pkl에서 ZIP 바이너리 데이터 역직렬화\n", | |
| " with open(input_pkl, \"rb\") as f:\n", | |
| " zip_binary = pickle.load(f)\n", | |
| "\n", | |
| " # 2. 바이너리 데이터를 임시 ZIP 파일로 쓰기\n", | |
| " with open(self.temp_zip, \"wb\") as f:\n", | |
| " f.write(zip_binary)\n", | |
| "\n", | |
| " # 3. ZIP 파일 압축 해제\n", | |
| " with zipfile.ZipFile(self.temp_zip, 'r') as z:\n", | |
| " # 'audio.mp3'를 현재 디렉토리에 추출\n", | |
| " z.extract(\"audio.mp3\", path=\".\")\n", | |
| "\n", | |
| " # 4. 추출된 파일의 이름을 사용자가 지정한 이름으로 변경\n", | |
| " # 이미 동일한 이름의 파일이 있다면 덮어쓰기 위해 삭제 처리\n", | |
| " if os.path.exists(output_mp3):\n", | |
| " os.remove(output_mp3)\n", | |
| " os.rename(\"audio.mp3\", output_mp3)\n", | |
| "\n", | |
| " # 임시 ZIP 파일 삭제\n", | |
| " os.remove(self.temp_zip)\n", | |
| "\n", | |
| " print(f\"복원 완료: {input_pkl} -> {output_mp3}\")\n", | |
| "\n", | |
| "mp = MyPickler()" | |
| ], | |
| "metadata": { | |
| "id": "XGkZv2Ul1PfL", | |
| "executionInfo": { | |
| "status": "ok", | |
| "timestamp": 1753235512812, | |
| "user_tz": -540, | |
| "elapsed": 14 | |
| } | |
| }, | |
| "execution_count": 3, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "##### use" | |
| ], | |
| "metadata": { | |
| "id": "cvCe2ysF0vWY" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "HMxUHrP6z8nj", | |
| "executionInfo": { | |
| "status": "ok", | |
| "timestamp": 1753235513604, | |
| "user_tz": -540, | |
| "elapsed": 792 | |
| }, | |
| "outputId": "d0a07daa-4156-4675-a37a-94de0ecf3910" | |
| }, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "직렬화 완료: GoodCharlotte_TheAnthem_88․8to89bpm_drumreduced.mp3 -> hidden_audio.pkl\n", | |
| "복원 완료: hidden_audio.pkl -> restored.mp3\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "mp.mp3_to_my_pkl(\"GoodCharlotte_TheAnthem_88․8to89bpm_drumreduced.mp3\", \"hidden_audio.pkl\")\n", | |
| "mp.my_pkl_to_mp3(\"hidden_audio.pkl\", \"restored.mp3\")" | |
| ] | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment