uploaded from Google CoLab

This commit is contained in:
Hansly Saw 2022-04-29 15:53:47 +08:00
parent ecfaf3326e
commit d71218b95b
1 changed files with 214 additions and 0 deletions

214
Kpop'minal.ipynb Normal file
View File

@ -0,0 +1,214 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Kpop'minal.ipynb",
"provenance": [],
"collapsed_sections": [],
"private_outputs": true,
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/buzz-lightsnack-2007/Kpopminal/blob/main/Kpop'minal.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"# Kpop'minal\n",
"Watch k-pop music videos in an ASCII format. \n",
"\n",
"_Uses [`video-to-ascii` by joelibaceta](https://github.com/joelibaceta/video-to-ascii)_. "
],
"metadata": {
"id": "TK6pOEjtZCFB"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "NKYqnAEwCPYG"
},
"outputs": [],
"source": [
"# KPOP'MINAL\n",
"# Watch k-pop music videos in an ASCII format. \n",
"\n",
"import os\n",
"import subprocess\n",
"from google.colab import files\n",
"from google.colab import output\n",
"\n",
"# selections\n",
"class selection: \n",
" artist = \"Twice\" #@param [\"Blackpink\", \"Twice\"]\n",
" video = \"\" #@param {type:\"string\"}\n",
" videoURL = ''\n",
"\n",
"class configs: \n",
" #@markdown ## Configuration\n",
" method = \"ascii-color\" #@param [\"filled-ascii\", \"ascii-color\", \"just-ascii\"]\n",
"\n",
"# Set screen formatting. \n",
"class formatting:\n",
" clear = '\\033[0m'\n",
" class color: \n",
" purple = '\\033[95m'\n",
" cyan = '\\033[96m'\n",
" cyan_dark = '\\033[36m'\n",
" blue = '\\033[94m'\n",
" green = '\\033[92m'\n",
" yellow = '\\033[93m'\n",
" red = '\\033[91m'\n",
" class font:\n",
" bold = '\\033[1m'\n",
" underline = '\\033[4m'\n",
"\n",
"class info: # screen messages\n",
" def status(message):\n",
" print(formatting.font.bold + 'Status: \\t' + formatting.clear + message)\n",
" def err(message):\n",
" print(formatting.font.bold + formatting.color.red + 'Error: \\t\\t' + formatting.clear + formatting.clear + message)\n",
" def warning(message):\n",
" print(formatting.font.bold + formatting.color.yellow + 'Warning: \\t' + formatting.clear + formatting.clear + message)\n",
" def success(message):\n",
" print(formatting.font.bold + formatting.color.green + 'Success: \\t' + formatting.clear + formatting.clear + message)\n",
"\n",
"# Install apt programs and python modules. \n",
"def get_dependencies(): \n",
" import subprocess\n",
"\n",
" def install_dependencies_apt(): \n",
" return os.system('apt update && apt install -y ffmpeg libasound-dev portaudio19-dev libportaudiocpp0 libportaudio2 python-pyaudio python3-pyaudio && apt clean && apt autoremove -y')\n",
"\n",
" def install_dependencies_pip():\n",
" return os.system('pip install \"video-to-ascii==1.2.8\" && pip install pyaudio')\n",
"\n",
" info.status('Setting up the environment…')\n",
"\n",
" install_dependencies_apt()\n",
" install_dependencies_pip()\n",
"\n",
" info.success('The environment has been set up. ')\n",
"\n",
"def download_video(): \n",
" musicvideos = {\n",
" # Download links for the music videos. Good thing it's available in archive.org. \n",
" \"Twice\": {\n",
" # Download links available here: https://archive.org/details/twuniverse\n",
" \"Like Ooh-Ahh\": \"https://ia804501.us.archive.org/7/items/twuniverse/Like%20Oohahhoohahh.ia.mp4\",\n",
" \"Cheer Up\": \"https://ia904501.us.archive.org/7/items/twuniverse/Cheer%20Up%20.ia.mp4\",\n",
" \"TT\": \"https://ia804501.us.archive.org/7/items/twuniverse/Tt%20.ia.mp4\",\n",
" \"Knock Knock\": \"https://ia904501.us.archive.org/7/items/twuniverse/Knock%20Knock%20.ia.mp4\",\n",
" \"Signal\": \"https://ia804501.us.archive.org/7/items/twuniverse/Signal%20.ia.mp4\",\n",
" \"One More Time\": \"https://ia804501.us.archive.org/7/items/twuniverse/One%20More%20Time.ia.mp4\",\n",
" \"Likey\": \"https://ia904501.us.archive.org/7/items/twuniverse/Likey%20.ia.mp4\",\n",
" \"Merry & Happy\": \"https://ia904501.us.archive.org/7/items/twuniverse/Merry%20%20Happy%20.ia.mp4\",\n",
" \"Candy Pop\": \"https://ia804501.us.archive.org/7/items/twuniverse/Candy%20Pop.ia.mp4\",\n",
" \"What Is Love?\": \"https://ia804501.us.archive.org/7/items/twuniverse/What%20Is%20Love%20.ia.mp4\",\n",
" \"Wake Me Up\": \"https://ia804501.us.archive.org/7/items/twuniverse/Wake%20Me%20Up.ia.mp4\",\n",
" \"I Want You Back\": \"https://ia804501.us.archive.org/7/items/twuniverse/I%20Want%20You%20Back.ia.mp4\",\n",
" \"Dance The Night Away\": \"https://ia904501.us.archive.org/7/items/twuniverse/Dance%20The%20Night%20Away%20.ia.mp4\",\n",
" \"BDZ\": \"https://ia904501.us.archive.org/7/items/twuniverse/Bdz.ia.mp4\",\n",
" \"Alcohol-Free\": \"https://ia804501.us.archive.org/7/items/twuniverse/Alcoholfree%20.ia.mp4\"\n",
" }\n",
" }\n",
"\n",
" def check_valid_input():\n",
" try:\n",
" # It should raise an exception if the person did not type the correct music video name. \n",
" if musicvideos[selection.artist][selection.video]: \n",
" selection.videoURL = musicvideos[selection.artist][selection.video]\n",
" \n",
" except:\n",
" info.err('The video that you are looking for does not exist. Make sure you have typed it properly. If you did, you can safely assume that this script is not updated. ')\n",
" os.kill(os.getpid(), 9)\n",
" \n",
" def start_download():\n",
" def check_exists(which=\"new\"):\n",
" if which == \"old\": return os.system(\"cat video.old.mp4\") == 0\n",
" else: return os.system(\"cat video.mp4\") == 0\n",
"\n",
" # Start downloading the video. \n",
" \n",
" os.system('rm -rf sample_data -v')\n",
" if check_exists(): os.system('rm -rf video.old.mp4 -v && mv video.mp4 video.old.mp4 -v')\n",
"\n",
" info.status('Downloading the music video…')\n",
"\n",
" if os.system('curl ' + selection.videoURL + ' -o video.mp4') > 0:\n",
" info.err('The download has failed, please re-run this script again. ')\n",
" os.system('rm -rf video.mp4 -v')\n",
" if check_exists(\"old\"): os.system('mv video.old.mp4 video.mp4 -v')\n",
" os.kill(os.getpid(), 1)\n",
" else:\n",
" info.success('The video was successfully downloaded.')\n",
" \n",
" check_valid_input()\n",
" start_download()\n",
"\n",
"\n",
"def render(): \n",
" def check_file_exists(which=\"new\"):\n",
" if which == \"old\": return (os.system('cat output.old.sh') == 0)\n",
" else: return (os.system('cat output.sh') == 0)\n",
"\n",
" def attemptReplace():\n",
" if check_file_exists(): os.system('rm -rf output.old.sh && mv output.sh output.old.sh -v')\n",
"\n",
" def process():\n",
" info.status('Processing the video. This will take some while.')\n",
" !video-to-ascii -f video.mp4 -o output.sh --strategy {configs.method}\n",
" \n",
" def check_success():\n",
" if check_file_exists():\n",
" output.clear()\n",
" info.success('The music video has been successfully processed. ')\n",
" else:\n",
" if check_file_exists(\"old\"): os.system('mv output.old.sh output.sh -v')\n",
" info.err('Unfortunately, an error may have ocurred during processing, and we apologize for the inconvenience. Kindly check the log for full details.')\n",
" os.kill(os.getpid(), 1)\n",
"\n",
" attemptReplace()\n",
" process()\n",
" check_success()\n",
"\n",
"def download_results():\n",
" info.status('Downloading the output…')\n",
" try:\n",
" files.download('output.sh')\n",
" except:\n",
" info.err('The file could not be downloaded, but it is available for manual downloading at the files view in the sidebar.')\n",
" else:\n",
" info.success('The file was succesfully downloaded.')\n",
"\n",
"\n",
"def main():\n",
" get_dependencies()\n",
" download_video()\n",
" render()\n",
" download_results()\n",
"\n",
"main()"
]
}
]
}