1
votes

I'm trying to find out wether a mediafile "could" be played back in VLC using LibVLC-Python.

In my python script I parse recursively through a directory (containing media and non-media-files as well as images etc.), opening and playing one file after another in VLC. Then I try to analyse, if it can actually be played with the vlc-functions will_play() and get_state(). This is highly unreliable though and the script has to pause in order to fully load the file. If an audio-file for example is very short and the script pauses to long it will not be detected as "playable" since it's playback has already stopped etc. If the script runs across a JPG it hangs up and sometimes text- and pdf-files will be labeled "will_play" :-( So far I was not able to use vlc-classes such as MediaTrackInfo()

Is there a way to just parse each file and determine i.e. by it's codec if VLC could play it? I just want to sort through huge directories and copy out "real" mediafiles (audio and video) that are not corrupted.

Here's my original script:

import os, sys, inspect, time

vlcpfad = "C:\Program Files (x86)\VideoLAN\VLC"

if not vlcpfad in sys.path:
    sys.path.append(vlcpfad)  
import vlc

# Get name and path of the script
pfadkomplett = os.path.realpath(os.path.abspath(inspect.getfile(inspect.currentframe())))
pfad = os.path.split(pfadkomplett)[0]
skriptname = os.path.split(pfadkomplett)[1]

# walk path
for pfad, unterordner, dateien in os.walk(pfad):
    for dateiname in dateien:
        # skip script itself
        if dateiname == skriptname: continue

        dateipfad = os.path.join(pfad, dateiname)

        p = vlc.MediaPlayer(dateipfad)          
        p.audio_toggle_mute()
                    p.play()

        # Wait a bit, so vlc can start playback
        time.sleep(0.2)

        while str(p.get_state()) == "State.Opening":
            time.sleep(0.1)

        print(dateipfad + ": " + str(p.will_play()))

        p.stop()
        del p
1

1 Answers

2
votes

One way is to test if media is OK before play it :

[...]
p = vlc.MediaPlayer(dateipfad)
media = p.get_media() 
media.parse() #get media info
if media.get_duration():
   # your is OK
else:
   # media NOK