Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

[open] how to define media_player_vlc (set video width/height + set loop)

edited January 2014 in OpenSesame

hello,

i'm trying to show a video in the middle of the screen and (at the same time) show text at the bottom of the screen.
therefore i tried to set video width/height.

another question is to loop a video until keyresponse.

is there any way to implement this?

thanks a lot,

ben

Comments

  • edited 3:57AM

    Hi Ben,

    i'm trying to show a video in the middle of the screen and (at the same time) show text at the bottom of the screen. therefore i tried to set video width/height.

    If you want to do (relatively) complex things with video, such as custom scaling and drawing additional things on top of the video surface, you're probably best of writing a custom script with opencv. This is not as difficult as it sounds, and a working example is provided here:

    another question is to loop a video until keyresponse.

    If you are using an inline script anyway, than this too would probably be best handled through a loop in your script.

    If you are not familiar with Python, quite a few excellent tutorials are available:

    Cheers and good luck!
    Sebastiaan

  • edited 3:57AM

    thanks!

    i downloded the opencv-package at opencv.org but the debugger says: "ImportError: No module named cv"

    copying the opencv-folder into opensesame-plugin-folder couldnt fix the problem...

    unfortunatly the link on your "video playback" section to opencv is broken for a while (http://opencv.willowgarage.com/documentation/python/index.html)

    do you have a hint for me?

    cheers,

    ben

  • edited January 2014

    Hi Boris,

    Basically, the example is a bit outdated. It uses cv, whereas the current module (and the one supplied with OpenSesame) is cv2, for OpenCV 2.

    The script below shows how to show how to play back a video using cv2. As you can see, it's actually a bit simpler than with cv. Note that this requires the legacy back-end.

    And thanks for pointing out the broken link.

    Cheers!
    Sebastiaan

    Update: Fixed error in script

    import cv2
    import numpy
    import pygame
    # Full path to the video file
    path = exp.get_file('myvideo.avi')
    # Open the video and determine the video dimensions
    video = cv2.VideoCapture(video)
    # A loop to play the video file. This can also be a while loop until a key
    # is pressed. etc.
    for i in range(100):
        # Get a frame
        retval, frame = video.read()
        # Rotate it, because for some reason it otherwise appears flipped.
        frame = numpy.rot90(frame)
        # The video uses BGR colors and PyGame needs RGB
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        # Create a PyGame surface
        surf = pygame.surfarray.make_surface(frame)
        # Now you can draw whatever you want onto the PyGame surface!
        pygame.draw.rect(surf, (255,0,0), (100, 100, 200, 200))
        # Show the PyGame surface!
        exp.surface.blit(surf, (0, 0))
        pygame.display.flip()
    
  • edited January 2014

    thanks! unfortunatly another problem occurred:

    File "dist\numpy\lib\twodim_base.py", line 157, in rot90
    Python traceback:
    ValueError: Input must >= 2-d.
    

    ???

    i don`t want to make any changes in "twodim_base.py" or do i have to?

    cheers,

    ben

  • edited 3:57AM

    Ow right, I see. The script didn't specify which file should be read. Basically, this ...

    video = cv2.VideoCapture()
    

    ... should be ...

    video = cv2.VideoCapture(path)
    
  • edited January 2014

    ah ok! makes sense! but heres another debug-message:

    AttributeError: 'experiment' object has no attribute 'surface'
    

    i couldnt fix the line:

    # Show the PyGame surface!
    exp.surface.blit(surf, (0, 0))
    pygame.display.flip()
    

    damn, i have no clue of python...sorry!

  • edited 3:57AM

    Hi Ben,

    To make this script work (as I already wrote above!), you need to use the legacy back-end, or the xpyriment back-end with 'Use OpenGL' set to 'no' under back-end settings. The reason is that this script uses certain PyGame functionality that is incompatible with an OpenGL-based method of display presentation.

    Cheers!
    Sebastiaan

  • edited 3:57AM

    I already used the xpyriment back-end with 'Use OpenGL' set to 'no'.

    After switching to legacy back-end the video plays :) Didn't knew that just one of these two options works.

    now i tried to let the video play in a while loop until keypress...no success :(

    sorry, i am into from php-programming but its hard for me to understand the python logic...

    thanks,

    ben

  • edited 3:57AM

    Hi Ben,

    If you are not familiar with Python, there are a lot of good free tutorials to get you started. For most things, you won't need any deep Python knowledge, so don't worry. But some knowledge will be necessary if you want to do semi-complicated things with video presentation.

    To get you started, below is an example of how you can probe the keyboard and break the video loop when a key is pressed. For more information, see:

    And indeed, the script didn't work with the xpyriment back-end. I added a small check to fix this, it's just a matter of selecting the right 'surface'.

    Cheers!
    Sebastiaan

    import cv2
    import numpy
    import pygame
    from openexp.keyboard import keyboard
    # Create a keyboard object
    my_keyboard = keyboard(exp, timeout=0)
    # Get the PyGame surface in a way that works for the legacy back-end, as well as
    # the xpyriment back-end with 'Use OpenGL' set to 'no'.
    if self.get('canvas_backend') == 'legacy':
        surface = exp.surface
    elif self.get('canvas_backend') == 'xpyriment':
        surface = exp.window
    else:
        raise Exception('This script requires legacy or xpyriment (no opengl)')
    # Full path to the video file
    path = exp.get_file('myvideo.avi')
    # Open the video and determine the video dimensions
    video = cv2.VideoCapture(path)
    # A loop to play the video file. This can also be a while loop until a key
    # is pressed. etc.
    while True:
        # Get a frame and break the loop if we failed, usually because the video
        # ended.
        success, frame = video.read()
        if not success:
            break
        # Rotate it, because for some reason it otherwise appears flipped.
        frame = numpy.rot90(frame)
        # The video uses BGR colors and PyGame needs RGB
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        # Create a PyGame surface
        surf = pygame.surfarray.make_surface(frame)
        # Now you can draw whatever you want onto the PyGame surface!
        pygame.draw.rect(surf, (255,0,0), (100, 100, 200, 200))
        # Show the PyGame surface!
        surface.blit(surf, (0, 0))
        pygame.display.flip()
        # Poll the keyboard and break the loop if a key was returned.
        key, time = my_keyboard.get_key()
        if key != None:
            break
    
  • edited February 2014

    the video was flipped in left/right direction so i had to add fliplr before rot90:

    frame = numpy.fliplr(frame)
    frame = numpy.rot90(frame)
    

    some further questions:

    • the video plays about 2times faster as normal? sample-frequence in back-end-settings and video sample-frequence are equal.

    • there is no sound?

    any ideas? thanks, ben

  • edited February 2014

    the video was flipped in left/right direction so i had to add fliplr before rot90:

    Yes that might be, I think it depends on the format. Just flip and rotate until it looks right!

    the video plays about 2times faster as normal? sample-frequence in back-end-settings and video sample-frequence are equal.

    You need to determine the framerate yourself in this script. So if it's too fast, just add a self.sleep() somewhere in the loop.

    there is no sound?

    Right, this is video only. Perhaps you misunderstood the script, so just to avoid confusion: It retrieves image frames from the video file and puts them on the screen. That's all, it's really very basic. It's not a real video engine that controls the framerate or handles audio.

  • edited 3:57AM

    what if it plays to slow ?

  • edited 3:57AM

    what if it plays to slow ?

    That's possible, because there's quite a bit of processing going on, and not in a very optimized way. I'm afraid the only real solution would be to use a lower resolution video. (Or think of a more efficient script that does the same thing, but that might not be trivial.)

Sign In or Register to comment.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games