Building a switch to play back files in different programs depending on file size?

A question for the scripting aces here in the forum, since i don't think it can be done using the regular commands. Since the MP4 format has gained a lot of usage for music files, playing back films, that also use MP4 is a little bit cumbersome. I am using a modifier key to open those files, since meanwhile i have a lot more MP4 music files than movie files in that format.

Just for convenience, is it possible to code a script, that would open files, say, over 700MB in program A, like VLC, while anything else would be opened in program B, like AIMP?

People tend to use a different extension for MP4 audio-only files, e.g. .m4a.

We're planning to expose the count of video and audio streams as part of the metadata object, so in the future a script could use that to decide what to do with an MP4 file. Not ready quite yet though.

Fair enough. It is not really a big deal, but i frequently forget to hold down the modifier key, starting films audio only. And yes, you have already given me advice i could rename those files in question to M4a once, which works. On the other hand, i would prefer to avoid that for consistency with my backup disks, and sometimes it's also fun to watch the video part, cover art etc., even though my music player currently doesn't support video playback. I would use some modifier key for that much rarer usecase, if needed. Thanks a lot.

You should really consider using m4a extension for audio files, that's the only non-hacky way.

That said, here is the script you've envisioned:

@script jscript
function OnClick(clickData)
{
	var SIZE_LARGE_FILE_THRESHOLD = 15728640; // 15 MiB
	var PATH_SMALL_FILE_PROGRAM = "C:\\Program Files\\AIMP\\AIMP.exe /ADD_PLAY";

	var file = clickData.func.sourcetab.selected_files[0];
	if (!file) {
		DOpus.Output("No file selected."); // Can happen when testing.
		return;
	}
	
	if (file.size.val > SIZE_LARGE_FILE_THRESHOLD) { // Video
		clickData.func.command.RunCommand("{allfilepath$}");
	}
	else { // Audio
		clickData.func.command.RunCommand(PATH_SMALL_FILE_PROGRAM + " {allfilepath$}");
	}
}

To have it automatically open large MP4 files in whatever the default program is (e.g., VLC) and small files in AIMP, simply paste this script here: File Types / MP4 (use filter to find it quicker) -- double-click it to open the editor / Commands tab / Events tab / Left double-click => and then paste this as the jscript function there.

By default the script considers the files above 15 MiB as videos and smaller than that as audio.

Thanks, bytespiller. I keep getting some error messages. Note, that i have changed the size considerably, because i have lots of very large MP4 audio files, maybe some even being larger than 150MB, for example whole albums. The same happens, when i do not change the size.

grafik

Add @script jscript on a new line at the top of the code.

(Only needed in File Types, where we haven’t added a language dropdown yet.)

1 Like

Good call Leo, I've edited my post now to include it.

2 Likes

Not sure what causes the issue. :thinking:
What could be wrong with the file size? Checking back tonight, thank yous.

grafik

I think you have no file selected. Paste my code again, I've added a check for it.

Looks like the size is not known/defined, which is strange when it's a file.

Does it only go wrong with certain files or locations, or all files?

I would also make a couple of changes to the code (although these won't help with the main issue):

  1. Add quotes around the program path:

    var PATH_SMALL_FILE_PROGRAM = "\"C:\\Program Files\\AIMP\\AIMP.exe\" /ADD_PLAY";
    
  2. Change the ...RunCommand("{allfilepath$}") line to this:

    clickData.func.command.RunCommand("FileType ACTION=shellex");
    

    (That should work better if this is ever used on more than one file, although it's not that important if it's only used when double-clicking a single file.)

That would have been embarrassing. But ... once i double click the file, even it wasn't selected, i would select it by the double click, wouldn't i? Very strange. Does it work for you, using your own players? I even tried to change it to VLC (with Leo's modified lines), but then i get this:

I tested a few MP4 movie files, in different locations, NTFS, but i get the same result, even after the changes you suggested. And, what i just tested, the music MP4s also throw the same error. Just to be sure, this is the current code. The path to AIMP is also correct, as i fetched it using Opus. Changing the player to VLC, however, brought up a different error code, which might be a hint. VLC is, by the way, installed on C:\program files, standard way, whereas AIMP is on my other SSD. But that shouldn't really make a difference.

scratching head

I'd like to add, that the extensions field says .m4v, but the drop down menu also lists mp4, as well as mp4v, which i guess is basically all the same.

Ah, try changing the [0] to (0) - Does that fix it?

Yes, that did it so far, music playback works now :+1:. However, AIMP seems to insist also taking the larger video files (which should be opened in 5KPlayer). Maybe the script should explicitely tell Opus to open this path:

C:\Program Files (x86)\DearMob\5KPlayer\5KPlayer.exe

even though i think 5KPlayer is set as the default video player.

Find this line:

clickData.func.command.RunCommand("FileType ACTION=shellex");

(Or clickData.func.command.RunCommand("{allfilepath$}"); if it's still like the original script.)

And change it to:

clickData.func.command.RunCommand("\"C:\\Program Files (x86)\\DearMob\\5KPlayer\\5KPlayer.exe\" {allfilepath$}");

That should work, at least with most players. Some might require extra command-line arguments but most don't.

Leo & bytespiller, it works now perfectly, please share the 'solved' credits!

Maybe i still need to adjust the upper size a bit, but the script does it's job now just fine.

:beers: :beers: :+1:

2 Likes

Glad to hear it! The original script works perfectly for me (I also use the VLC and AIMP), I have no idea why it wouldn't work for you. Perhaps I have some funky beta version of Opus or something :smiley:

Great job, thanks!