A memorandum on how to build a Linux environment (Termux) on an Android device and save YouTube and Niconico videos as local files.
● Operating Environment
Device: Xperia 1VI
OS: Android 16
Termux: Google Play Store version (the F-Droid version should also work)
Required packages (within Termux): ffmpeg, python, yt-dlp, nodejs
● How to use
Execute in the format: alias_name URL.
Example:
ytv https://www.youtube.com/watch?v=xxxx
You can save videos from YouTube as local files with this.
● Installation method
Install Termux
↓
termux-setup-storage
Execute (internal storage permission)
↓
pkg install ffmpeg python yt-dlp nodejs
↓
Create alias
● About creating aliases
At first, I created templates in a notepad and copied and pasted them, but since the commands became long and cumbersome when using several options, I created aliases to make shortcuts.
nano ~/.bashrc
Execute to open bashrc (write alias settings here)
Contents of bashrc
# ytv: Save video from YouTube as mp4 (with embedded thumbnail)
# yta: Save audio only from YouTube as mp3
# ytnv: Save video from Niconico as mp4 (with embedded thumbnail)
# ytna: Save audio from Niconico as mp3 (with embedded thumbnail)
alias ytv='yt-dlp --embed-thumbnail --convert-thumbnails jpg --js-runtimes node -f "bv[ext=mp4]+ba[ext=m4a]/bv+ba/best" --merge-output-format mp4 -o "/storage/emulated/0/Movies/ytdlp-movie/%(title)s [%(id)s].%(ext)s"'
alias yta='yt-dlp --js-runtimes node -f bestaudio --extract-audio --audio-format mp3 --audio-quality 0 -o "/storage/emulated/0/Music/ytdlp-audio/%(title)s [%(id)s].%(ext)s"'
alias ytnv='yt-dlp --cookies ~/.config/yt-dlp/niconico-login-cookie.txt --js-runtimes node --embed-thumbnail --convert-thumbnails jpg -o "/storage/emulated/0/Movies/ytdlp-movie/%(title)s [%(id)s].%(ext)s"'
alias ytna='yt-dlp --cookies ~/.config/yt-dlp/niconico-login-cookie.txt --js-runtimes node -f bestaudio --extract-audio --audio-format mp3 --audio-quality 0 -o "/storage/emulated/0/Music/ytdlp-audio/%(title)s [%(id)s].%(ext)s"'
Save with CTRL O → Enter → CTRL X
source ~/.bashrc hash -r
Execute to apply changes.
○ Explanation of options
--embed-thumbnail
Embed thumbnail
--convert-thumbnails jpg
Convert thumbnail to jpg
--js-runtimes node
Execute JavaScript processing with Node.js
-f "bv[ext=mp4]+ba[ext=m4a]/bv+ba/best"
Prioritize mp4 video + m4a audio
--merge-output-format mp4
Set final output to mp4
-o "/storage/.../%(title)s [%(id)s].%(ext)s"
Specify save location and filename
● About acquiring and maintaining login-cookies for Niconico
--cookies ~/.config/yt-dlp/niconico-login-cookie.txt
The part related to this.
Since some Niconico videos require a login, I specify the login cookie in yt-dlp.
○ About acquisition
Use the Firefox extension, cookies.txt, to convert it into a text file.
After logging into Niconico, switch to the PC view on the top page and execute cookies.txt. Get the text file via Download. Please change the save location as you prefer.
○ About maintenance
Login cookies have an expiration date, so they need to be updated when they expire. The following is my environment.
/storage/emulated/0/yt-dlp-maintenance/niconico-login-cookie.txt
# Backup and master. Update the content by overwriting the file with the same name. When updating the cookie, change only the content of this file and save it.
Open the PC version of Niconico in Firefox, copy and paste from the extension "Export cookies", and overwrite.
cp /storage/emulated/0/yt-dlp-maintenance/niconico-login-cookie.txt ~/.config/yt-dlp/
Execute this.
source ~/.bashrc is not required (it is applied immediately because the file is referenced)