DrLazer
DrLazer

Reputation: 3113

Unable to convert a string to and integer with python in fabric

This is driving me nuts. I am using fabric to do some video processing tasks on a remote server via SSH.

I make a call to ffprobe on the remote machine to get the info back on the video that I will have sent over to that server before we enter this function. I am trying to parse out the bit about Duration: from the output of ffprobe so I can get the length of the video in seconds. This is the troublesome part of the function where I cannot seem to convert a string to an integer.

with cd("~/videos"):
   command = "ffprobe  "
   command += videoNameOnly
   output = run(command)
   durationIndex = output.find("Duration:")
   durationIndex = durationIndex + 10
   duration = output[durationIndex:]
   durationFrags = duration.split(":")
   print "====================================="
   print durationFrags[0].strip()
   print durationFrags[1].strip()
   print durationFrags[2].strip()
   hours = int(durationFrags[0].strip())

The output from this is as follows

00
00
52.90, start
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/fabric/main.py", line 712, in main
    *args, **kwargs
  File "/usr/local/lib/python2.7/dist-packages/fabric/tasks.py", line 298, in execute
    multiprocessing
  File "/usr/local/lib/python2.7/dist-packages/fabric/tasks.py", line 197, in _execute
    return task.run(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/fabric/tasks.py", line 112, in run
    return self.wrapped(*args, **kwargs)
  File "/home/amyface/fabfile.py", line 26, in main
    encode();
  File "/home/amyface/fabfile.py", line 34, in encode
    runJobs(jobs,cursor)
  File "/home/amyface/fabfile.py", line 60, in runJobs
    getMiddleFrame(sourceVideo,videoNameOnly,cursor)
  File "/home/amyface/fabfile.py", line 96, in getMiddleFrame
    hours = int(durationFrags[0].strip())
ValueError: invalid literal for int() with base 10: '\x1b[0m\x1b[0;39m\x1b[0m\x1b[0;39m\x1b[0m\x1b[0;39m00'
Disconnecting from [I have removed the server name from here]

How come it wont let me convert "00" into an integer? Also what is the strange value in the error after "with base 10: "?

Upvotes: 2

Views: 646

Answers (3)

pbacterio
pbacterio

Reputation: 1152

The command executed (ffprobe) is writing terminal color codes.

Try telling this program to not use them by setting the AV_LOG_FORCE_NOCOLOR environment variable

Upvotes: 0

joaquin
joaquin

Reputation: 85603

The key is this:

ValueError: invalid literal for int() with base 10:  '\x1b[0m\x1b[0;39m\x1b[0m\x1b[0;39m\x1b[0m\x1b[0;39m00'

As indicated here these are ANSI terminal codes. In the string, \x1b are ESC characters

You could use a regex as shown in the answers of the above link to remove unwanted codes and extract the last '00'

Upvotes: 1

brjaga
brjaga

Reputation: 399

I don't know anything about fabric, but according to the ValueError, durationFrags[0].strip() is '\x1b[0m\x1b[0;39m\x1b[0m\x1b[0;39m\x1b[0m\x1b[0;39m00', not '00'. Does this produce output that's designed to go to a terminal? Those look like ANSI escape codes to me.

Upvotes: 1

Related Questions