1

I am working on a script that opens media files in VLC Media Player with one of the os.exec* methods. I am using Python v3.6 on Windows 10.

VLC opens with an error stating that the file cannot be opened, however, the path to the file is also wrong. It shows the file's path starting with my home directory followed by the last portion of the file name separated by a space.

Example:

  • I have a video at the path D:\videos\SuperCoolVideo - Part1.mp4
  • VLC attempts to open this video at the path C:\Users\user\Part1.mp4 instead

The code I am using is as follows:

import os

MEDIA_PLAYER = 'C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe'
video = 'D:\\videos\\SuperCoolVideo - Part1.mp4'
os.execv(MEDIA_PLAYER, [video]) # or os.execl(MEDIA_PLAYER, video)

The error from VLC:

Your input can't be opened:
VLC is unable to open the MRL 'fd://0'. Check the log for details.

Your input can't be opened:
VLC is unable to open the MRL 'file:///C:/Users/user/Part1.mp4'. Check the log for details.

I think (but am not entirely sure) this is because I must define the path of the video or escape the path properly, but I have been unsuccessful in my research on how to handle passing the path properly to VLC.

I have also tried using os.system() to no avail. I get the following error message:

The filename, directory name, or volume label syntax is incorrect.


* NOTE: I should add that I cannot use subprocess.call('path\to\vlc.exe') because I need the script to end immediately after VLC is opened. Using subprocess.call() causes the script to remain running until I close VLC.

Jesse
  • 69
  • 1
  • 7
  • It's probably because of the spaces, try adding quotation marks around your path. – Hans Lehnert Jul 06 '18 at 00:27
  • @HansLehnert I can confirm that I have had no luck after attempting to open paths wrapped in quotes :( – Jesse Jul 06 '18 at 00:35
  • You used `video = '"path"'` with both single and double quotes, right? Do you still get the same error message from VLC? – Hans Lehnert Jul 06 '18 at 00:43
  • Well, similarly to this yes. Because the path is retrieved dynamically, I had to use `'"{}"'.format(path)`, or alternatively, `'"'+path+'"'`. This lead me to believe this could be a VLC path configuration problem, but I don't want to rule out that it could be solved if my code was revised. – Jesse Jul 06 '18 at 00:53
  • Windows is its own very odd bird, but on UNIXlike operating systems, your list starts at `argv[0]`, so if you want the argument to be `argv[1]`, you'd need to use `os.execv(MEDIA_PLAYER, [MEDIA_PLAYER, video])`. – Charles Duffy Jul 06 '18 at 00:55
  • This is explicitly given in the `os` module docs; quoting: *In either case, the arguments to the child process should start with the name of the command being run, but this is not enforced.* (It's not enforced, but that doesn't mean the program you're running will see an argument you passed in the location for the program's own name). – Charles Duffy Jul 06 '18 at 00:55
  • 1
    @CharlesDuffy @HansLehnert Charles, thanks referring to that point in the docs. I made a few attempts and ended up getting the desired output by combining both you and Hans' advice. The args passed to `os.execv` required quotes to be recognized properly, and I did have to pass the program again as `argv[0]`. **The solution was:** `os.execv(MEDIA_PLAYER, ['"'+MEDIA_PLAYER+'"', '"'+video+'"'])` – Jesse Jul 06 '18 at 01:43
  • @Jesse, feel free to add that as an answer with the "Add an Answer" button. (BTW, on UNIX, the literal quotes would do more harm than good, so that part is definitely a Windows-ism; it might be worth editing to specify the platform a little more prominently in the question, so folks don't try to apply it elsewhere). – Charles Duffy Jul 06 '18 at 02:37

1 Answers1

0

I got it working properly thanks to the help of Hans Lehnert and Charles Duffy in the question's comments above. Two changes were necessary.

  1. On Windows, it is necessary to wrap quotes around any arguments that may contain spaces passed to os.execv. However, this can be harmful on UNIX-based systems, so do not bother if your are using one. Examples → '"'+path+'"' or '"{}"'.format(path)
  2. The first argument must, once again, be your executing process. I will just quote Charles' comment as he explained it well.

    Windows is its own very odd bird, but on UNIX-like operating systems, your list starts at argv[0], so if you want the argument to be argv[1], you'd need to use os.execv(MEDIA_PLAYER, [MEDIA_PLAYER, video])."

Solution

So together, I opened files properly in VLC by wrapping arguments in quotes and passing the executable as the first argument in os.execv:

os.execv(MEDIA_PLAYER, ['"'+MEDIA_PLAYER+'"', '"'+video+'"'])

Jesse
  • 69
  • 1
  • 7