{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "source": [ "# YouTube video to audio\n", "\n", "- Author: Pierre Guillou\n", "- Blog post: [Video-to-Audio | A free Web APP to get mp3 audio file from any YouTube video](https://medium.com/@pierre_guillou/video-to-audio-a-notebook-and-web-app-to-get-mp3-audio-file-from-any-youtube-video-bb6a1c85390d)\n", "- Date: 06/12/2023 (Revision: 13/05/2025)\n", "\n" ], "metadata": { "id": "GGMgtwNm8W6i" } }, { "cell_type": "code", "source": [ "!pip install gradio>=3.50.2\n", "!pip install yt-dlp>=2023.3.4\n", "!pip install ffmpeg-python>=0.2.0" ], "metadata": { "id": "wEbrJ9i3nnwP" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "import gradio as gr\n", "import subprocess\n", "import os\n", "import tempfile\n", "import re\n", "import sys\n", "\n", "# Check if running in Colab\n", "IN_COLAB = 'google.colab' in sys.modules\n", "\n", "# Install required packages if in Colab\n", "if IN_COLAB:\n", " print(\"Running in Google Colab. Installing required packages...\")\n", " !pip install -q gradio yt-dlp ffmpeg-python\n", "\n", "def download_youtube_mp3(video_url):\n", " \"\"\"\n", " Downloads the audio from a YouTube video in MP3 format using yt-dlp.\n", "\n", " Args:\n", " video_url (str): The YouTube video URL.\n", "\n", " Returns:\n", " tuple: (status, result) where status is a boolean indicating success and result is either the MP3 file path or an error message\n", " \"\"\"\n", " try:\n", " # Create a temporary directory for the download\n", " temp_dir = tempfile.mkdtemp()\n", "\n", " # Options for yt-dlp\n", " output_filename_template = os.path.join(temp_dir, \"%(title)s.%(ext)s\")\n", "\n", " command = [\n", " \"yt-dlp\",\n", " \"-x\", # Extract audio\n", " \"--audio-format\", \"mp3\",\n", " \"--audio-quality\", \"0\", # 0 for best VBR quality\n", " \"-o\", output_filename_template,\n", " video_url\n", " ]\n", "\n", " # Add cookies from browser if in Colab (helps with authentication)\n", " if IN_COLAB:\n", " command.insert(1, \"--cookies-from-browser\")\n", " command.insert(2, \"chrome\")\n", "\n", " # Execute the command\n", " process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n", " stdout, stderr = process.communicate()\n", "\n", " if process.returncode == 0:\n", " # Find the MP3 file in the temporary directory\n", " for file in os.listdir(temp_dir):\n", " if file.endswith(\".mp3\"):\n", " mp3_path = os.path.join(temp_dir, file)\n", " return True, mp3_path\n", " return False, \"Error: MP3 file not found after conversion\"\n", " else:\n", " error_output = stderr.decode('utf-8', errors='ignore')\n", "\n", " # Check if it's a YouTube authentication error\n", " if \"Sign in to confirm you're not a bot\" in error_output or \"cookies\" in error_output:\n", " if IN_COLAB:\n", " return False, \"YouTube requires authentication for this video. Try running the following command first to authenticate:\\n!yt-dlp --cookies-from-browser chrome -x --audio-format mp3 \" + video_url\n", " else:\n", " return False, \"YouTube requires authentication for this video. This app cannot download videos that require login. Try another video or use the app locally.\"\n", "\n", " return False, f\"Download error: {error_output}\"\n", "\n", " except Exception as e:\n", " return False, f\"An error occurred: {str(e)}\"\n", "\n", "# Gradio Interface\n", "def youtube_to_mp3(youtube_url):\n", " if not youtube_url:\n", " return \"Please enter a valid YouTube URL\"\n", "\n", " # Check if the URL is valid\n", " if not re.match(r'^(https?://)?(www\\.)?(youtube\\.com|youtu\\.?be)/.+$', youtube_url):\n", " return \"Invalid YouTube URL. Please enter a URL in the format https://www.youtube.com/watch?v=...\"\n", "\n", " success, result = download_youtube_mp3(youtube_url)\n", "\n", " if success and os.path.isfile(result):\n", " return result\n", " else:\n", " return result # This is an error message\n", "\n", "# Create the interface\n", "demo = gr.Interface(\n", " fn=youtube_to_mp3,\n", " inputs=gr.Textbox(label=\"YouTube Video URL\", placeholder=\"https://www.youtube.com/watch?v=...\"),\n", " outputs=gr.Audio(label=\"Downloaded MP3\") if IN_COLAB else gr.Textbox(label=\"Result\"),\n", " title=\"YouTube to MP3 Converter\",\n", " description=\"\"\"Download the audio from a YouTube video in MP3 format.\n", "\n", "**Important note**: \"\"\" + (\"Running in Google Colab. This environment can handle most YouTube videos, including those requiring authentication.\" if IN_COLAB else \"Due to YouTube restrictions, some videos requiring authentication cannot be downloaded in this Hugging Face Spaces environment. For full functionality, run this application locally or in Google Colab.\"),\n", " examples=[[\"https://www.youtube.com/watch?v=jNQXAC9IVRw\"]] # First YouTube video (Me at the zoo)\n", ")\n", "\n", "if __name__ == \"__main__\":\n", " # Launch parameters\n", " if IN_COLAB:\n", " demo.launch(debug=True)\n", " else:\n", " # Specific parameters for Hugging Face Spaces\n", " demo.launch(server_name=\"0.0.0.0\",\n", " server_port=7860,\n", " share=False,\n", " ssr_mode=False)\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 662 }, "id": "0jQjqFRjoH4W", "outputId": "ab16ed35-d380-43ae-f9c8-b1ee0c80f80c" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Running in Google Colab. Installing required packages...\n", "It looks like you are running Gradio on a hosted a Jupyter notebook. For the Gradio app to work, sharing must be enabled. Automatically setting `share=True` (you can turn this off by setting `share=False` in `launch()` explicitly).\n", "\n", "Colab notebook detected. This cell will run indefinitely so that you can see errors and logs. To turn off, set debug=False in launch().\n", "* Running on public URL: https://435fef6abed049176c.gradio.live\n", "\n", "This share link expires in 1 week. For free permanent hosting and GPU upgrades, run `gradio deploy` from the terminal in the working directory to deploy to Hugging Face Spaces (https://huggingface.co/spaces)\n" ] }, { "output_type": "display_data", "data": { "text/plain": [ "" ], "text/html": [ "
" ] }, "metadata": {} } ] } ] }