mps-youtube / yewtube

yewtube, forked from mps-youtube , is a Terminal based YouTube player and downloader. No Youtube API key required.
GNU General Public License v3.0
8.09k stars 644 forks source link

Mpv crashes mps-youtube, and MPlayer plays only 1mn. #1128

Closed galgot closed 1 year ago

galgot commented 3 years ago

Hi, as the title says, setting mpv as player and trying to read a track makes mpsyt quit with this error :

Traceback (most recent call last):
  File "/usr/local/bin/mpsyt", line 8, in <module>
    sys.exit(main.main())
  File "/usr/local/lib/python3.8/site-packages/mps_youtube/main.py", line 153, in main
    if matchfunction(i.function, i.regex, userinput):
  File "/usr/local/lib/python3.8/site-packages/mps_youtube/main.py", line 70, in matchfunction
    func(*matches)
  File "/usr/local/lib/python3.8/site-packages/mps_youtube/commands/play.py", line 102, in play
    g.PLAYER_OBJ.play(songlist, shuffle, repeat, override)
  File "/usr/local/lib/python3.8/site-packages/mps_youtube/player.py", line 79, in play
    self._playsong()
  File "/usr/local/lib/python3.8/site-packages/mps_youtube/player.py", line 138, in _playsong
    self._launch_player()
  File "/usr/local/lib/python3.8/site-packages/mps_youtube/player.py", line 298, in _launch_player
    self.launch_player(cmd)
  File "/usr/local/lib/python3.8/site-packages/mps_youtube/players/mpv.py", line 135, in launch_player
    self._player_status(self.songdata + "; ", self.song.length)
  File "/usr/local/lib/python3.8/site-packages/mps_youtube/players/mpv.py", line 188, in _player_status
    if resp['data'] is not None:
KeyError: 'data'

If setting player to MPlayer, it reads only 1 minute of the track. I also get this msg before it reads :

/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py:844: RuntimeWarning: line buffering (buffering=1) isn't supported in binary mode, the default buffer size will be used
  self.stdout = io.open(c2pread, 'rb', bufsize)

This is on a MacBook Pro running Mac OS X 10.11.6. Here infos on the mpsyt install : mpsyt version : 0.2.8 notes : released 17 February 2018 pafy version : 0.5.5 (youtube-dl backend) youtube-dl version : 2020.11.26 Python version : 3.8.5 (default, Sep 9 2020, 23:31:58) [Clang 8.0.0 (clang-800.0.42.1)] Processor : i386 Machine type : x86_64 Architecture : 64bit, Platform : macOS-10.11.6-x86_64-i386-64bit sys.stdout.enc : utf-8 default enc : utf-8 Config dir : /Users/galgot/.config/mps-youtube env:TERM : xterm env:SHELL : /bin/bash env:LANG : fr_FR.UTF-8

MPV is version 0.33.0 and MPlayer is 1.3.0-4.2.1 these are Macports installed.

galgot commented 3 years ago

Ok, let me make precisions about reading with MPlayer after some tests. It doesn't read exactly 1 mn. It's more that it just doesn't finish the track, stops before the end. Sometime it reads the whole track, but it's completely random...

Edit: Same problem as reported in #1117

Only, MPV can't come to the rescue in my case ... :/

justmeandopensource commented 3 years ago

I have the same problem. After the 2 or 3 attempts, the video plays.

darkhz commented 3 years ago

@galgot Hi there, I believe I solved the issue with mpv-related audio playback.

The (very small) fix has to be done across two files: $PATH_TO_PYTHON_LIB/python3.9/site-packages/mps_youtube/player.py $PATH_TO_PYTHON_LIB/python3.9/site-packages/mps_youtube/mpris.py

In player.py, BEFORE line 530, which shows the following snippet:

...
if resp.get('event') == 'property-change' and resp['id'] == 1:
    if resp['data'] is not None:
        elapsed_s = int(resp['data'])
...

Add this snippet: if 'data' in resp:

In mpris.py, BEFORE line 224, which shows the following snippet:

...
if resp.get('event') == 'property-change':
    self.setproperty(resp['name'], resp['data'])
...

Add this snippet: if 'data' in resp:

The code must look like this:

(player.py)

...
try:
            observe_full = False
            cmd = {"command": ["observe_property", 1, "time-pos"]}
            s.send(json.dumps(cmd).encode() + b'\n')
            volume_level = elapsed_s = None

            for line in s.makefile():
                resp = json.loads(line)

                # deals with bug in mpv 0.7 - 0.7.3
                if resp.get('event') == 'property-change' and not observe_full:
                    cmd = {"command": ["observe_property", 2, "volume"]}
                    s.send(json.dumps(cmd).encode() + b'\n')
                    observe_full = True

                if 'data' in resp:
                    if resp.get('event') == 'property-change' and resp['id'] == 1:
                        if resp['data'] is not None:
                            elapsed_s = int(resp['data'])

                elif resp.get('event') == 'property-change' and resp['id'] == 2:
                    volume_level = int(resp['data'])

                if(volume_level and volume_level != g.volume):
                    g.volume = volume_level
...

(mpris.py)

...
try:
            observe_full = False
            self._sendcommand(["observe_property", 1, "time-pos"])

            for line in self.socket.makefile():
                resp = json.loads(line)

                # deals with bug in mpv 0.7 - 0.7.3
                if resp.get('event') == 'property-change' and not observe_full:
                    self._sendcommand(["observe_property", 2, "volume"])
                    self._sendcommand(["observe_property", 3, "pause"])
                    self._sendcommand(["observe_property", 4, "seeking"])
                    observe_full = True

                if 'data' in resp:
                    if resp.get('event') == 'property-change':
                        self.setproperty(resp['name'], resp['data'])

        except socket.error:
            self.socket = None
            self.mpv = False

    def bindfifo(self, fifopath, mpv=False):
...

I believe this problem happened as a result of bugfixes to mpv (0.7.x). Do tell if this solves your problem or not.

There's another related issue with mpv, where the following error message comes every time you try playing a track:

Error parsing commandline option title: option requires parameter Make sure you're using e.g. '--title=value' instead of '--title value'

Aren't you facing this?I have a fix for this too, I'll post the patch in the proper issuetracker page.

Side note, I installed mps-youtube via pip.

(Sorry for the reposting, I encountered another error and wan't sure whether this patch was correct)

Masacaai commented 3 years ago

This is a duplicate of this issue. Apply the changes mentioned in that commit and it should start working as intended.