mpv icon indicating copy to clipboard operation
mpv copied to clipboard

Support for moving through files in the same directory.

Open LabMem765 opened this issue 13 years ago • 18 comments

Hey, I was just wondering if something was possible to do.

I remember in mplayer2, you could use HOME and END to, as they said, go to the "next/previous playtree entry in the parent list." I admit I do not completely understand what this means exactly, but I was able to use it for moving through files within the same folder. I did like this feature, but unless I'm missing something, there does not seem to be anything with the same functionality in mpv. I do know there are playlists, but that requires you to plan it out before you start anything, and it's even more annoying to make a playlist if you're running mpv via the app bundle.

Now, I did see in the mpv man that is seems the playtree from mplayer2 has been completely removed. So, I understand if this request turns out to be very annoying to implement or just plain impossible, in which case you can just disregard this.

LabMem765 avatar Jul 09 '13 19:07 LabMem765

The playtree was just a tree-structured playlist. In mpv, it's linear and flat.

What you describe must have been part of the OSX bundle functionality. mplayer2/mpv itself only add entries to the playtree/playlist if explicitly requested, such as mpv directory/*, in which case you can navigate the playlist normally.

The OSX bundle code is special and I know it had its own playlist/tree management. Maybe pigoz knows more?

ghost avatar Jul 09 '13 19:07 ghost

I tried just now to open a file in mplayer2 on OSX using the bundle and HOME/END don't do anything here. I was actually not even aware this feature even existed (if it did, it shouldn't be specific to OSX).

That said I'd like a "play next file in the same directory" feature as well.

pigoz avatar Jul 09 '13 20:07 pigoz

Well, this is embarrassing. My memory is foggy, so I got stuff wrong.

First of all, it seems that I was basing this on my experience of MPlayer.app, which is based off of MPlayer OS X Extended, and which uses a patched mplayer2 binary. The guy's website is https://sites.google.com/site/hermihg/, and you can get the source code of the whole app there (although you need to contact him directly for the patched mplayer2 binary source code).

Secondly, I got the controls wrong. HOME just returns you to the beginning of the playing file, which you can do here easily enough. END does do the functionality I thought it did, which is to "Advance to next file." Now, I don't see explicit playlist controls, so I guess this moves you forward in the playlist you made or it moves you ahead in the directory if you didn't make a playlist.

So, I guess we can check out the source code here, and hopefully figure something out.

LabMem765 avatar Jul 09 '13 20:07 LabMem765

Actually, maybe we could add some sort of "load all other files in a directory" options.

Here's a suggestion: if only one file is passed to the mpv command line, it will search for other files in the same directory, and insert them in the playlist before and after the file passed to mpv (using a sort order). Then it will play the next "natural" file if the file on command line has finished.

Alternative suggestion: add a command that tries to find the "next" file. It could search the current directory for a file that looks like the next episode (using a sort order). This one could be more aggressive and could even try to go up one directory level to find sibling directories containing potential successors.

Also, we have to wonder whether this is reallly needed compared to basic shell use, like mpv dir/*.

ghost avatar Jul 09 '13 21:07 ghost

I'd probably prefer the command way, most of the time I only want to play one (or certain) file(s) in a directory. There'd also need to be some file type filtering, to keep mpv from trying to play .nfo or .sfv files or stuff like that.

ChrisK2 avatar Jul 09 '13 21:07 ChrisK2

I do know there are playlists, but that requires you to plan it out before you start anything, and it's even more annoying to make a playlist if you're running mpv via the app bundle.

Sounds like another feature for a GUI.

dubhater avatar Jul 09 '13 21:07 dubhater

I like the alternative suggestion, add a command that tries to find the "next" file, I even have previous written c code (which I have been using for a long thine) that does this.

It takes a array of files path and the currently playing file's path and checks if there a next file. e.g if you had a file called Video - 01.mkv it would match variations of Video - 02.mkv such as Video 2 and Video_-_002.mkv

@Link0306 The code was even adapted into MPlayer OS X Extended, so I could possibly have written it, if it was added before that project was forked.

Bilalh avatar Jul 09 '13 21:07 Bilalh

By the way, I think this kind of stuff would be ideal as external Lua script, instead of putting it in the C core.

ghost avatar Jul 09 '13 21:07 ghost

By the way, I think this kind of stuff would be ideal as external Lua script, instead of putting it in the C core.

Yes. Definitely.

pigoz avatar Jul 09 '13 22:07 pigoz

Fair enough, how is the Lua branch going?

Bilalh avatar Jul 09 '13 22:07 Bilalh

I could actually merge it rather sooner than later.

My current plan is running Lua scripts in their own thread (so they can do whatever they want without interfering with video timing), but I realize that this also depends on libmpv (because you could reuse the communication code), and will take a while. So it might better to merge an imperfect version of Lua scripting support first.

ghost avatar Jul 09 '13 23:07 ghost

Yo, it's me again. While you guys are doing this, I'm looking through the MPlayer.app source code and trying to figure out how this feature works there. There's quite a lot of stuff and I'm not very experienced in it, but I'm making progress (or at least I think I am). If I figure out how it works, I'll report back here. It'll probably take me a few days at least though.

LabMem765 avatar Jul 10 '13 05:07 LabMem765

Alright, since who needs sleep anyway, here's what I found. I'll start by saying this seems to be written in Objective C, and it uses Cocoa, neither of which I have much experience with. Also, you can get the code I am using from here: http://dl.dropbox.com/u/5825800/MPlayer/Source/MPlayer_r14.1410s-src.zip

Now then, first we start in PlayerController.m, lines 1976-1977

else if ((keyHandled = ([theEvent keyCode] == kVK_End)))
            [menuController playlistNext:nil];

playlistPrevious and playlistNext are in the same spot, so onto them in MenuController.m, lines 74-92

- (IBAction) playlistPrevious:(NSMenuItem *)sender
{
    if ([[appController playerController] isRunning]) {
        if ([[appController playerController] isPlayingFromPlaylist])
            [[appController playListController] playPrevious:sender];
        else
      [[appController playerController] playSequentialReversed:YES];
    }
}

- (IBAction) playlistNext:(NSMenuItem *)sender
{
    if ([[appController playerController] isRunning]) {
        if ([[appController playerController] isPlayingFromPlaylist])
            [[appController playListController] playNext:sender];
        else
            [[appController playerController] playSequential];
    }
}

So, I'm guessing this code checks if there is an existing playlist or not, and chooses to go along the existing playlist or go along the directory. Anyway, I found playSequential and playSequentialReversed back in PlayerController.m, lines 841-870

// Find and play the next media file in the playingItem's containing directory
- (BOOL)playSequential {
  return [self playSequentialReversed:NO];
}

- (BOOL)playSequentialReversed:(BOOL)reversed {
    MovieInfo *info = [self playingItem];
    NSString *dir = [[info filename] stringByDeletingLastPathComponent];

    NSArray *listing = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dir error:NULL];
    NSEnumerator *dirEnum;

  if(reversed)
    dirEnum = [listing reverseObjectEnumerator];
    else
    dirEnum = [listing objectEnumerator];

    NSString *file;
    while((file = [dirEnum nextObject]) && [file isNotEqualTo:[[info filename] lastPathComponent]]) {}
    while((file = [dirEnum nextObject]) && ![[AppController sharedController] isExtension:[file pathExtension] ofType:MP_DIALOG_MEDIA]) {}
    if(file) {
        MovieInfo *item = [MovieInfo movieInfoWithPathToFile:[dir stringByAppendingPathComponent:file]];
        // The MPESequentialMovie setting is preserved in the new movie
        [[item prefs] setBool:[[movieInfo prefs] boolForKey:MPESequentialMovie] forKey:MPESequentialMovie];
        [self playItem:item];
        return YES;
    }   

    return NO;
}

And that's what I found. There could be (and probably is) more stuff in the code that should be seen for full understanding and whatnot, but this is what I was able to find so far.

LabMem765 avatar Jul 10 '13 09:07 LabMem765

Not really sure what this does... too much objective-c for me.

ghost avatar Jul 10 '13 12:07 ghost

Does the following:

  • gets directory of current file
  • lists directory
  • searches for the current playing item in the list
  • from there select the next file that is playable and play it

pigoz avatar Jul 10 '13 12:07 pigoz

My version from MPlayer OS X Extended https://github.com/sttz/MPlayer-OSX-Extended/blob/954af64a06d30be5898d191ddcbc66f7ada5b47d/source/controllers/MovieMethods.m#L184

which is written in mostly C, which might be easier to figure out.

It does the following

  • Lists the files in the current directory
  • parses the filenames
  • Compares the parsed filenames with current file's filepath
  • Return the file path of the next/previous video

It also works with variations. E.g if current file is Video - 01.mkv it would match variations of Video - 02.mkv as well like Video 2.mkv and Video_-_002.mkv

Bilalh avatar Jul 10 '13 13:07 Bilalh

FWIW, Matroska has support for "segment after current" and "segment before current". IMO, if that is coded up, then without these hints, mpv could manufacture a prev/next list using something based on the logic in the previous comment.

mathstuf avatar Oct 10 '13 00:10 mathstuf

This can now be handled with the autoload.lua script: https://github.com/mpv-player/mpv/blob/master/TOOLS/lua/autoload.lua

ghost avatar Apr 17 '15 18:04 ghost