0

like the many other threads I've opened, I am trying to create a multi-feature instant replay system utilizing the blackmagic hyperdeck which operates over Telnet. The current feature I am trying to implement is an in-out replay which requires storing two timecode variables in the format of hh:mm:ss;ff where h=hours, m=minutes, s=seconds, and f=frames @30fps. the telnet command for this is transport info, and the response returns 9 lines of which I only want the timecode from the 7th. Any idea on how to do this, as it is way out of my league?

status: stopped
speed: 0
slot id: 1
clip id: 1
single clip: false
display timecode: 00:00:09;22
timecode: 00:00:09;22
video format: 1080i5994
loop: false

Here's ideally what I would like it to look like

import telnetlib

host = "192.168.1.13" #changes for each device
port = 9993 #specific for hyperdecks
timeout = 10

session = telnetlib.Telnet(host, port, timeout)

def In():
    session.write(b"transport info \n")
    line = session.read_until(b";00",.5)
    print(line)
    #code to take response and store given line as variable IOin
def out():
    session.write(b"transport info \n")
    line = session.read_until(b";00",.5)
    print(line)
    #code to take response and store given line as variable IOout
def IOplay():
    IOtc = "playrange set: in: " + str(IOin) + " out: " + str(IOout) + " \n"
    session.write( IOtc.encode() )
    speed = "play: speed: " + str(Pspeed.get() ) + "\n"
    session.write(speed.encode() )

2 Answers2

0
def get_timecode(text):
    tc = ''
    lines = text.split('\r\n')
    for line in lines:
        var, val = line.split(': ', maxsplit=1)
        if var == 'timecode':
            tc = val
    return tc

You could choose to go directly to lines[6], without scanning, but that would be more fragile if client got out of sync with server, or if server's output formatting changed in a later release.

EDIT:

You wrote:

session.write(b"transport info \n")

#code to take response and store given line as variable IOin

You don't appear to be reading anything from the session. I don't use telnetlib, but the docs suggest you'll never obtain those nine lines of text if you don't do something like:

expect = b"foo"  # some prompt string returned by server that you never described in your question
session.write(b"transport info\n")
bytes = session.read_until(expect, timeout)
text = bytes.decode()
print(text)
print('Timecode is', get_timecode(text))
J_H
  • 17,926
  • 4
  • 24
  • 44
  • Show us the code you are currently running. I imagine there's a `print( ... )` statement in there which shows the nine lines you enclosed in your question. – J_H Jul 25 '19 at 23:54
  • Show us the code you're running that calls telnetlib. (You tagged your question with telnetlib, so surely you are importing that.) – J_H Jul 26 '19 at 00:04
  • Ok, I've now added a couple of lines that will return the timecode as the last piece of text, I just don't know how to extract that from the rest of the data... the return I'm getting is `b'500 connection info:\r\nprotocol version: 1.9\r\nmodel: HyperDeck Studio Mini\r\n\r\n208 transport info:\r\nstatus: stopped\r\nspeed: 0\r\nslot id: 1\r\nclip id: 1\r\nsingle clip: false\r\ndisplay timecode: 00:00:11;00'` – corneredphoton Jul 26 '19 at 01:21
  • You have a bytestring there. To recover the unicode text it is necessary to decode the bytes, as I did with: `text = bytes.decode()`. Then you can pass the string to `get_timecode` which will parse out the relevant line. – J_H Jul 26 '19 at 02:38
  • I'm getting invalid syntax on the = of `if var = 'timecode':` – corneredphoton Jul 26 '19 at 03:03
  • Sorry, fixed the == typo. – J_H Jul 26 '19 at 22:16
0

For the most part here's what I got to at least partially work

TCi = 1
TCo = 1
def In():
    global TCi
    session.write(b"transport info \n")
    by = session.read_until(b";00",.5)
    print(by)
    s = by.find(b"00:")
    TCi = by[s:s+11]
def Out():
    global TCo
    session.write(b"transport info \n")
    by = session.read_until(b";00",.5)
    print(by)
    s = by.find(b"00:")
    TCo = by[s:s+11]
def IOplay():
    IOtc = "playrange set: in: " + str(TCi) + " out: " + str(TCo) + " \n"
    print(IOtc.encode() )
    session.write(IOtc.encode() )
    speed = "play: speed: 2 \n"
    session.write(speed.encode() )

except that its encoding as

b"playrange set: in: b'00:00:01;11' out: b'00:00:03;10' \n"

rather than

"playrange set: in: 00:00:01;11 out: 00:00:03;10 \n"

I need to get rid of the apostrophe's and b prefix in front of the variables

Any ideas?