Skip to content

Conversation

@lenerd
Copy link
Contributor

@lenerd lenerd commented Apr 9, 2020

Hi all,

I have added a small feature to ncmpcpp which allows adding audio streams from websites via youtube-dl. So I would like to ask if there is interest in adding such a feature to the official sources (a similar suggestion appeared in issue #53). Otherwise I would just maintain this as a patch for myself.

"youtube-dl is a command-line program to download videos from YouTube.com". It also supports a lot of other sources, e.g., websites with podcasts, and media libraries of public broadcasters. While the main functionality of this tool is the download of videos, youtube-dl is also able to extract the URLs of the corresponding audio and audio/video streams, which can then be added to MPD's playlist.

This change adds a new action to ncmpcpp's playlist view: Pressing 'D' (in the default configuration), prompts for the URL of a website. The input is passed to youtube-dl to retrieve the URL of the audio stream and available metadata. If successful, the URL is added to the MPD playlist.

I use this to listen to radio programs or podcasts while having the controls of my usual music player. So I don't have to find some browser tab to pause the playback, and there is also no need to download/play/delete the audio files manually.

No new dependencies are introduced to compile ncmpcpp. To use the feature, youtube-dl is needed to be installed at run time (available in many distributions repositories or via pip). It is executed as a child process using Boost.Process and Boost.PropertyTree is used to parse its JSON output.

So, what do you think of this?

Of course, I am open for any suggestions, and I also have some questions:

  • I guess it would be a good idea to have some sort of timeout in case youtube-dl should hang (has not happen to me so far even without a network connection). Implementing this should be doable, but I am unsure of a good default value. Adding a long YouTube playlist can take a while.

  • Currently, the interface is blocked until the action is completed which might be a bit annoying. In principle, it could be run in a background thread, but I have no idea what parts of ncmpcpp are thread-safe.

  • Is there a way to "refresh" the playlist (view) during the execution of an action? In this way, progress could be shown when adding playlists. In a second ncmpcpp instance I can see the items being added one after another, but in the executing instance the new items appear only after the action has been completed.

Thanks!

@CamilleScholtz
Copy link

Does it actually pre-download the video/audio, or does it stream it on the go? That would maybe fix the playlist timeout thing

@lenerd
Copy link
Contributor Author

lenerd commented Apr 13, 2020

Does it actually pre-download the video/audio, or does it stream it on the go?

The call to youtube-dl just extracts the URL(s) from the webpage. The download happens by MPD on the server-side.

Edit: Even though the audio itself is not downloaded on the client-side, a number of HTTP requests are made which can take a while for large playlists.

@lenerd lenerd force-pushed the lenerd/youtube-dl branch from 279ee83 to f37fd5c Compare June 30, 2020 13:57
@Salastil
Copy link

Salastil commented Nov 2, 2020

There has been a general slowdown of work done on the main ytdl project and a few forks have popped up, one of which is some 700 commits ahead of the main project. Perhaps it would be prudent to put a config option to select the name of the ytdl binary? There seems to be 3 competing projects at the moment. I have aliased youtube-dlc to youtube-dl but there may come a point where users may have two competing versions of the project installed at once.

@gottaeat
Copy link

the patch applies and builds fine, but i have to set $LANG to C otherwise it prompts locale::facet::_s_create_c_locale name not valid.

@lenerd
Copy link
Contributor Author

lenerd commented Nov 20, 2020

Hi,
@mssx86 I cannot reproduce this with any of the locales I have installed. Which locale are you usually using, and what did you do exactly to trigger the error message?

@lenerd
Copy link
Contributor Author

lenerd commented Nov 20, 2020

@Salastil I suppose one could add an option is there is demand for this. Otherwise one could always adjust PATH. Do all of the projects maintain the same command line interface?

@Salastil
Copy link

@lenerd As it stands right now youtube-dlc: https://github.com/blackjack4494/yt-dlc just symbolically links youtube-dl to the youtube-dlc binary. Youtube-dl appears to have returned from its DMCA strikedown status and a quick glance at the diff for the two youtube extractors shows that the two projects are beginning to diverge. youtube-dlc at the moment is superior for the wider community of sites like Dailymotion, Peertube, Soundcloud etc as its some 700 commits ahead but some users may end up with both youtube-dl and youtube-dlc installed at the same time which would break the symbolic link and the option to just set it via PATH would be impossible. I don't believe that youtube-dlc has changed any command line flags, just updated the extractors to be functional.

@lenerd lenerd force-pushed the lenerd/youtube-dl branch 2 times, most recently from 1c298e6 to fb95546 Compare December 14, 2020 07:59
@lenerd lenerd force-pushed the lenerd/youtube-dl branch 2 times, most recently from ac9340c to 5e58fdc Compare December 23, 2020 17:34
if (id == -1) {
return;
}
if (title.has_value()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does it try to add multiple things from the database?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

youtube-dl has support for playlists and outputs one line of JSON per title. Each of those is parsed and added to the MPD playlist.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but why the output of youtube-dl is used to add items from the database to queue? It seems unexpected that it would do anything else than add youtube links to queue.

src/actions.cpp Outdated
// search the youtube-dl executable in the PATH
auto ydl_path = bp::search_path("youtube-dl");
if (ydl_path.empty()) {
Statusbar::ScopedLock slock;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to show a message for a while, use Statusbar::print(f). Statusbar::put is for prompts (with ScopedLock) or for showing a message while input is blocked.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, good to know!

@lenerd lenerd force-pushed the lenerd/youtube-dl branch 2 times, most recently from c8bfa27 to ecd4f16 Compare December 28, 2020 19:18
@lenerd
Copy link
Contributor Author

lenerd commented Dec 28, 2020

@arybczak Thanks for your comments!

I adjusted and extended the status messages.

@arybczak
Copy link
Collaborator

arybczak commented Jan 24, 2021

How is it supposed to be used? Can you show a few examples?

lenerd added 2 commits April 22, 2021 21:03
youtube-dl [0] is a "command-line program to download videos from
YouTube.com" and many other websites, e.g. media libraries of public
broadcasters.

This commit adds a new action to ncmpcpp's playlist view: Pressing 'D',
prompts for the URL of a website.  The input is passed to youtube-dl to
retrieve the URL of the audio stream and available metadata.  If
successful, the URL is added to the MPD playlist.

[0]: https://github.com/ytdl-org/youtube-dl
- use `Statusbar::print{,f}` instead of `Statusbar::put` for
  non-blocking status messages
- display number of items added
- display of non-zero exit codes of youtube-dl
@lenerd lenerd force-pushed the lenerd/youtube-dl branch from ecd4f16 to 0847e59 Compare April 22, 2021 19:03
// -f bestaudio/best: selects the best available audio-only stream, or
// alternatively the best audio+video stream
bp::ipstream output;
bp::child child_process(ydl_path, url, "-j", "-f", "bestaudio/best", bp::std_out > output,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bp::child child_process(ydl_path, url, "-j", "-f", "bestaudio/best","--playlist-end", "100" , bp::std_o ++ut > output,

Just a quality of life change here, when adding autogenerated playlist it will add 1000 items to ncmpcpp. Adding in --playlist end 100 will trim that number down to a more sensible 100. The videos are only good for something like 3-6 hours before they need to expire and need to be regenerated so it is effectively impossible to listen to the full 1000 item playlist anyway and it takes a good bit of time for it to generate such a huge playlist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants