Skip to content

Commit

Permalink
Merge pull request #2311 from jupyter-naas/2310-bugfix-python-convert…
Browse files Browse the repository at this point in the history
…-audiofile

fix: Added generic ffmpeg package helper and updated sample wav file
  • Loading branch information
FlorentLvr authored Oct 21, 2023
2 parents 2fb5305 + 33f46d9 commit 491b40b
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ dmypy.json

# template data files
YahooFinance/STOCK_CLUSTERS.csv
*.xz

# template outputs
YahooFinance/STOCK_CLUSTERS.png
105 changes: 83 additions & 22 deletions Python/Python_Convert_audiofile_from_wav_to_mp3.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"tags": []
},
"source": [
"**Author:** Mohit Singh"
"**Author:** [Mohit Singh](), [Akshat Katiyar]()"
]
},
{
Expand All @@ -53,7 +53,7 @@
"tags": []
},
"source": [
"**Last update:** 2023-04-12 (Created: 2023-03-08)"
"**Last update:** 2023-10-21 (Created: 2023-03-08)"
]
},
{
Expand Down Expand Up @@ -100,31 +100,60 @@
"outputs": [],
"source": [
"!wget \"https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-amd64-static.tar.xz\"\n",
"!tar xf ffmpeg-git-amd64-static.tar.xz\n",
"!mv ffmpeg-git-20230313-amd64-static ffmpeg"
"!tar xf ffmpeg-git-amd64-static.tar.xz"
]
},
{
"cell_type": "markdown",
"id": "4af95176-f480-4200-88a6-ee75c066626b",
"metadata": {},
"source": [
"### Find the ffmpeg package binary filename"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "de44748f-c70f-4a9e-ac11-c183b79d4b94",
"id": "568e293d-8bcd-4806-bc4c-c49f826055ec",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"import os\n",
"new_path = os.path.join(os.getcwd(), \"ffmpeg\")\n",
"%env PATH=$new_path"
"\n",
"def find_directory_starting_with_ffmpeg_git():\n",
" # Get the current directory\n",
" current_directory = os.getcwd()\n",
"\n",
" # List all directories in the current directory\n",
" directories = [d for d in os.listdir(current_directory) if os.path.isdir(os.path.join(current_directory, d))]\n",
"\n",
" # Check for directories starting with \"ffmpeg-git\"\n",
" matching_directories = [d for d in directories if d.startswith(\"ffmpeg-git\")]\n",
" return matching_directories\n",
"\n",
"dir_path = find_directory_starting_with_ffmpeg_git()\n",
"package_name = None\n",
"if len(dir_path) > 1:\n",
" print(\"Multiple ffmpeg directory found using the first one\")\n",
"package_name = dir_path[0]\n",
"mv_package = f\"mv {package_name} ffmpeg\"\n",
"!rm -rf ffmpeg\n",
"!{mv_package}"
]
},
{
"cell_type": "markdown",
"id": "a808cf8b-c61c-4651-86c4-8be456c79b53",
"id": "924ae217-e8c9-4543-80e5-ca0accf3c7f4",
"metadata": {
"papermill": {},
"tags": []
"execution": {
"iopub.execute_input": "2023-10-15T03:37:08.099633Z",
"iopub.status.busy": "2023-10-15T03:37:08.099401Z",
"iopub.status.idle": "2023-10-15T03:37:08.102882Z",
"shell.execute_reply": "2023-10-15T03:37:08.101924Z",
"shell.execute_reply.started": "2023-10-15T03:37:08.099606Z"
}
},
"source": [
"### Import libraries"
Expand All @@ -133,16 +162,15 @@
{
"cell_type": "code",
"execution_count": null,
"id": "2ff36cda-6873-4cf5-aa6c-eeb499b250d8",
"id": "0d743d71-e781-469d-bbf0-78b168ead9c8",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"try:\n",
" from pydub import AudioSegment\n",
"except:\n",
"except Exception:\n",
" !pip install pydub\n",
" from pydub import AudioSegment\n",
"import requests"
Expand All @@ -156,8 +184,13 @@
"tags": []
},
"source": [
"### Setup Variables\n",
"- `audio_file_url`: Link of audio file in wav to be converted to mp3"
"### Setup variables\n",
"**Mandatory**\n",
"- `audio_file_url`: Link of audio file in wav to be converted to mp3.\n",
"\n",
"**Optional**\n",
"- `wav_path`: File path to be saved in wav in local.\n",
"- `mp3_path`: File path to be saved in mp3 in local."
]
},
{
Expand All @@ -170,12 +203,11 @@
},
"outputs": [],
"source": [
"# Inputs\n",
"## link of sample wav file\n",
"audio_file_url = 'https://file-examples.com/storage/fef1706276640fa2f99a5a4/2017/11/file_example_WAV_1MG.wav'\n",
"wav_path = 'audio.wav'\n",
"# Mandatory\n",
"audio_file_url = 'https://www2.cs.uic.edu/~i101/SoundFiles/BabyElephantWalk60.wav'\n",
"\n",
"##Output\n",
"# Optional\n",
"wav_path = 'audio.wav'\n",
"mp3_path = 'audio.mp3'"
]
},
Expand All @@ -190,6 +222,35 @@
"## Model"
]
},
{
"cell_type": "markdown",
"id": "8f3d9990-b53d-44be-8a3b-afb22524a1cc",
"metadata": {
"execution": {
"iopub.execute_input": "2023-10-17T02:36:39.604370Z",
"iopub.status.busy": "2023-10-17T02:36:39.604096Z",
"iopub.status.idle": "2023-10-17T02:36:39.607629Z",
"shell.execute_reply": "2023-10-17T02:36:39.606939Z",
"shell.execute_reply.started": "2023-10-17T02:36:39.604301Z"
}
},
"source": [
"### Append ffmpeg package to PATH environment variable"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f3f0b9f9-552a-4298-91f5-2dbc43172163",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"new_path = os.path.join(os.getcwd(), \"ffmpeg\")\n",
"os.environ['PATH'] += os.pathsep + new_path"
]
},
{
"cell_type": "markdown",
"id": "2164cdac-70bc-47d4-bcbb-6d3c2fed6aba",
Expand Down Expand Up @@ -320,4 +381,4 @@
},
"nbformat": 4,
"nbformat_minor": 5
}
}

0 comments on commit 491b40b

Please sign in to comment.