Seb A
Seb A

Reputation: 21

How to get the frame from the vlc media player

I'm currently using vlc library for python (python-vlc) to get the video stream from a sdp stream describe with a sdp file. I'm currenctly using this code :

import vlc
import time 

instance = vlc.Instance()
player = instance.media_player_new()
media = instance.media_new("./bebop.sdp")
player.set_media(media)

player.play()
time.sleep(10)

Which works well to display the video. But I don't want to just display it, I want to use each frame from the video to make some image processing on it and then to display the modified frames.

I've read almost all the documentation and diverse post on forums but I can't find a way to do this.

I use vlc to get the video stream because open cv can't open it because of some ffmpeg error.

Upvotes: 2

Views: 8142

Answers (3)

Rolf of Saxony
Rolf of Saxony

Reputation: 22443

Get the frames per second from the video or default to 25 as a reasonable guess, then step through the paused video, using the frames per second as your jump forward each time.

import vlc
import time

instance = vlc.Instance('--no-xlib --quiet')
player = instance.media_player_new()
media = instance.media_new("./bebop.sdp"")
player.set_media(media)
player.play()
mfps = int(1000 / (player.get_fps() or 25))
player.set_time(30000) # start at 30 seconds
player.pause()
t = player.get_time()
for iter in range(30):
    t += mfps
    player.set_time(t)
    if player.get_state() == 3:
        player.pause()
    time.sleep(0.5)

Here, I start at the 30 second mark and then advance just 30 times.
You will probably want to keep advancing until player.get_state() == 6 indicating that the video has finished. [state 3 = playing]

Upvotes: 1

Degang Guo
Degang Guo

Reputation: 505

# -*- coding: utf-8 -*-

import vlc
import time 

def mspf(mp):
    """Milliseconds per frame"""
    return int(1000 // (mp.get_fps() or 25))


if __name__ == "__main__":
    instance = vlc.Instance()
    player = instance.media_player_new()
    media = instance.media_new("1.m4v")
    player.set_media(media)

    player.play()

    """Play 800th frames"""
    new_time = 800 * mspf(player)
    player.set_time(new_time)

    time.sleep(10)

Upvotes: 0

Related Questions