Python Forum
How to pause/interupt with keyboard
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to pause/interupt with keyboard
#1
Am currently building up 2 sets of files. One set is the audio/WAV and the other set is the transcription. In needing to check the accuracy of the transcription, the following script plays each of the WAV files.

#!/usr/bin/python3

import vlc
import time
import glob

wav_files = glob.glob("*.wav")
instance=vlc.Instance(["--no-sub-autodetect-file"])

# You should not recreate a player for each file, just reuse the same 
# player
player=instance.media_player_new()

for wav in wav_files:
    player.set_mrl(wav)
    player.play()
    playing = set([1,2,3,4])
    time.sleep(5) #Give time to get going
    duration = player.get_length() / 1000
    mm, ss = divmod(duration, 60)
    print("Playing", wav, "Length:", "%02d:%02d" % (mm,ss))
    while True:
        state = player.get_state()
        if state not in playing:
            break
        continue
I'm using the package python-vlc from https://github.com/oaubert/python-vlc , and thanks to Olivier Aubert for helping me with an error message issue with subtitles. The messages were of the form:

Error:
[00007f29d4024158] core demux error: option sub-original-fps does not exist [00000000010c1368] core input error: no suitable demux module for `file/subtitle:///....' [00007f29d00037e8] core demux error: option sub-original-fps does not exist [00000000010d5028] core input error: no suitable demux module for `file/subtitle:///....'
and the line of code that suppressed those messages is:

instance=vlc.Instance(["--no-sub-autodetect-file"])
So, now back to the question. The script is doing what it should and I will add some code to, before each WAV file is played, to print/display the actual transcription for that WAV file.

But if the transcription does not equal the spoken word (audio), then I want to add some code to pause the script (e.g. space bar) so that some details can be written down. Ideally, it would be nice to replay just that WAV to confirm the transcription to the spoken word, but for now, just a pause will suffice.
Reply
#2
I know this is a little old.

Maybe using keyboard.is_pressed as shown in reply #4 in https://python-forum.io/Thread-keyboard-...bute-error can be the solution.

Note that I'm totally new to python, so you might have to give your own interpretation to it.
Reply
#3
(Mar-18-2018, 11:03 AM)sterretje Wrote: I know this is a little old.

Maybe using keyboard.is_pressed as shown in reply #4 in https://python-forum.io/Thread-keyboard-...bute-error can be the solution.

Note that I'm totally new to python, so you might have to give your own interpretation to it.

Thanks, I will have a look at the keyboard.is_pressed function. I'm not sure how to control things within the loop though. The outside loop is simply displaying text then play am audio. If a key is pressed I want the script to pause, so that if the key is pressed again, the script continues. It is to give time to note down any errors, and then continue processing.
Reply
#4
You are probably going to need a prompt prior to using keyboard.is_pressed() as sterretje suggested.

Perhaps using an 'input' statement would be a little simpler:
programPause = input("Press the <Enter> key to CONTINUE.")
Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply
#5
psutil has a suspend() and a resume function(). I have only used them with a multiprocessing Process and so suspend or resume the process upon certain conditions, so don't know how they work without using a Process.
Reply
#6
Thanks for your help "ljmetzger" and "woooee" - there is a 'play', 'pause' in vlc if you use the 'getch' code. The script is working fine now ..

#!/usr/bin/python3

import vlc
import time
import glob
from subprocess import call
import sys
import termios, tty

try:
    from msvcrt import getch  # try to import Windows version
except ImportError:
    def getch():  # getchar(), getc(stdin)  #PYCHOK flake
        fd = sys.stdin.fileno()
        old = termios.tcgetattr(fd)
        try:
            tty.setraw(fd)
            ch = sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old)
        return ch

wav_files = glob.glob("*.wav")
instance=vlc.Instance(["--no-sub-autodetect-file"])

# You should not recreate a player for each file, just reuse the same
# player
player=instance.media_player_new()

for wav in wav_files:
    text_filename = (wav.replace(".wav" , ".txt" , 4))
    print(text_filename)
    print('\n' * 10)
    call(["cat", (text_filename)])
    print('\n' * 10)
    
    player.set_mrl(wav)
    player.play()
    playing = set([1,2,3,4])
    time.sleep(1) #Give time to get going
    duration = player.get_length() / 1000
    mm, ss = divmod(duration, 60)
    print("Playing", wav, "Length:", "%02d:%02d" % (mm,ss))
    
    while True:
        state = player.get_state()
        if state not in playing:
            break
        k = getch()
        if k in ["N","n"]:#Next
            player.stop()
            break
        elif k in ["Q","q"]:#Quit
            player.stop()
            sys.exit()
            break
        elif k == " ":#Toggle Pause
            player.pause()
        else:
            print("[Q - Quit, N - Next, Space - Pause]")
        continue
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I pause only one object? actualpy 1 356 Feb-01-2024, 07:43 PM
Last Post: deanhystad
  How to pause execution (on windows)? pstein 1 552 Jun-23-2023, 06:58 PM
Last Post: rob101
  Is it Possible to pause a script? tester_V 6 1,491 Apr-05-2023, 06:11 AM
Last Post: tester_V
  Did subprocess.Popen() causes main routine to pause stdout? liudr 4 3,617 May-04-2021, 08:58 PM
Last Post: liudr
  Not able to make a specific thing pause in pygame cooImanreebro 4 3,203 Dec-13-2020, 10:34 PM
Last Post: cooImanreebro
  How to Make Python code execution pause and resume to create .csv and read values. Kashi 2 3,753 Jun-14-2018, 04:16 PM
Last Post: DeaD_EyE
  how to pause pyxhook keylogger johweb 0 2,366 Jun-13-2018, 03:17 PM
Last Post: johweb
  Pause event Trajme 2 3,091 Dec-06-2017, 11:01 PM
Last Post: hshivaraj
  how to enter a pause in the middle of a file sylas 2 3,272 Jun-11-2017, 06:46 AM
Last Post: sylas

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020