How do I configure mpv and yt-dlp so that when I watch a youtube video with mpv, it will use sponsorblock? I tried to set --sponsorblock-mark=all into ~/.config/yt-dlp/yt-dlp.conf but it doesn't work. I also have script-opts=ytdl_hook-ytdl_path=yt-dlp in ~/.config/mpv/mpv.conf.
Assistant
To configure mpv and yt-dlp to use SponsorBlock when watching YouTube videos, you need to make sure that the yt-dlp is invoked with the correct options and that mpv is properly calling yt-dlp. Here's a step-by-step guide:
Ensure yt-dlp is Installed and Updated:
Make sure you have yt-dlp installed and updated to the latest version, as it includes support for SponsorBlock.
User
Nope, doesn't work.
I have yt-dlp installed
I have renamed the yt-dlp config to be named config and it has the correct sponsorblock config defined.
mpv is configured to using yt-dlp and it seems to run it correctly (but not pass the correct args)
Doesn't work
Assistant
If mpv appears to be using yt-dlp but not passing the correct arguments for SponsorBlock, you might need to set up a more explicit configuration within mpv. Here’s a more detailed approach to ensure the SponsorBlock arguments are passed correctly:
User
I'm using nixos, so replacing it with which yt-dlp is not so straightforward. Is it?
Assistant
On NixOS, the way executables are handled is somewhat different due to the packaging and environment management philosophy of Nix. The binaries are typically located in paths that are not standard system-wide, which can complicate things like scripts that rely on these paths.
User
With the script, it says
[ytdl_hook] ERROR: [generic] '/home/x/bin/sponsorblock.sh' is not a valid URL
Assistant
The error message suggests that mpv is trying to interpret the script path itself as a URL, which means it's not correctly recognizing the shell script wrapper as an executable command that should be run with . Here are a few ways to troubleshoot and resolve this issue:
bash
pip install -U yt-dlp
Configure yt-dlp for SponsorBlock:
The --sponsorblock-mark=all option is correct, but you need to ensure that yt-dlp is called with it. Add this option to your ~/.config/yt-dlp/config file (note the file path is config, not yt-dlp.conf).
bash
# ~/.config/yt-dlp/config
--sponsorblock-mark=all
Configure mpv to Use yt-dlp:
Set ytdl_hook-ytdl_path correctly in your mpv configuration so mpv knows where to find yt-dlp. Additionally, ensure that ytdl_hook-add_metadata=no is not set, as it might interfere.
Replace /path/to/yt-dlp with the actual path to the yt-dlp executable, which you can find by running which yt-dlp in your terminal.
Enable YTDL Hook Script:
Make sure the ytdl_hook.lua script, which allows mpv to stream videos directly via youtube-dl or yt-dlp, is enabled. This is default, but you can explicitly check for it in ~/.config/mpv/scripts/.
Test Your Configuration:
Open a YouTube URL in mpv:
bash
mpv https://www.youtube.com/watch?v=your_video_id
While playing, you should see SponsorBlock segments being skipped or marked, depending on their type.
If this setup doesn't work as expected, double-check the paths and permissions of the configuration files and ensure there are no conflicting options. Keep an eye on the terminal output when you launch mpv to catch any potential errors.
Verify yt-dlp Path: Make sure the path to yt-dlp in your mpv.conf is correct. You can verify the correct path using which yt-dlp in your terminal, and then specify this path explicitly:
Replace /usr/local/bin/yt-dlp with the actual output from which yt-dlp.
Custom Script for Additional Arguments:
The default ytdl_hook.lua script may not pass custom arguments from the yt-dlp config file. To resolve this, you can create a custom shell script wrapper that appends the necessary yt-dlp options and configure mpv to use this script.
Make sure that script support is enabled in mpv, as it relies on the ytdl_hook script to use yt-dlp. This should be default, but check any custom configurations that might disable scripting.
Verify Execution:
Run mpv with verbose logging using the --msg-level=all=v option to ensure your wrapper is being used:
Look for log entries showing the command that mpv uses to call yt-dlp and ensure that --sponsorblock-mark=all is included.
This method should ensure that mpv uses yt-dlp with the additional SponsorBlock arguments. If it doesn't, checking the verbose logs should help identify what is going wrong in the argument passing.
In your specific case, if you're using NixOS, you often deal with executables through the Nix store and environment setup. Here's how you can handle this situation:
Find the yt-dlp Executable Path:
If you installed yt-dlp via Nix, its path can be found by running the following command in a Nix shell where yt-dlp is available:
bash
command -v yt-dlp
This will give you the full path to the yt-dlp executable in the Nix store. Use this path in your script wrapper or mpv configuration.
Create a Wrapper Using Environment Variables:
If yt-dlp is available in the environment when you start your mpv, you can use environmental variables to help it find the executable:
Wrapper Script:
bash
#!/bin/bash
yt-dlp --sponsorblock-mark=all "$@"
Ensure this script is executable:
bash
chmod +x /path/to/yt-dlp-wrapper.sh
For the above script to work seamlessly, ensure that yt-dlp is in the PATH when mpv uses it. If you are not sure, you can specify the full path as found with command -v yt-dlp.
Use mpv’s Environment:
Ensure whatever environment setup you have makes yt-dlp accessible to mpv. This typically involves launching mpv from a shell or desktop environment where yt-dlp has been made available, usually through a nix-shell or similar method, ensuring that your shell has yt-dlp present in the PATH.
Configure mpv to Use the Wrapper Script:
Edit your mpv configuration to ensure the script is pointing to the correct path:
Launch mpv and verify that the SponsorBlock options are being passed by observing the console output or ensuring segments are skipped.
By setting up a dedicated environment for running mpv with the necessary configuration, you can avoid issues related to dynamic paths in NixOS's declarative environment. If the above methods still do not work as expected, ensure that any shell or environmental peculiarities specific to your NixOS configuration are adjusted to expose the necessary binaries to mpv.
yt-dlp
Double-Check Script Configuration
Ensure the Script is Executable:
Verify that the wrapper script (let's call it sponsorblock.sh) is executable:
bash
chmod +x /home/x/bin/sponsorblock.sh
Correct the Wrapper Script:
The wrapper script should correctly execute yt-dlp. Here's a basic template:
Ensure that mpv is running in an environment where your shell script and yt-dlp are accessible. If you launch mpv from the command line, make sure the terminal session has yt-dlp available.
Alternative: Fully Qualify the Command
Another approach, especially if the wrapper script idea continues to be problematic, is to avoid the script and directly configure mpv if yt-dlp is accessible in your PATH:
Check Direct Execution:
Try running yt-dlp with your options directly in a terminal to ensure it functions as expected: