0

I would like to take a text document that contains Feet+Frames values and convert them to Timecodes. For example, 0000+00 is replaced with 00:00:00:00.

I have a function to do the conversion from Feet+Frames to Timecode (16 frames per foot, framerate is 24 frames per second):

def FeetFramesToTimecode(FeetFrames):
    frames = int(FeetFrames[:4])*16+int(FeetFrames[-2:])
    return "%02d:%02d:%02d:%02d" % (frames/(3600*framerate), frames/(60*framerate)%60, frames/framerate%60, frames%framerate)

And then I have code that goes through the text file and sub's the Feet+Frame with timecode:

for line in input_document:
    found = re.search(r'\d{4}\+\d{2}', line)
    if found:
        print "%s replaces %s" % (FeetFramesToTimecode(found.group()), found.group())
        new_line = re.sub(r'\d{4}\+\d{2}', (FeetFramesToTimecode(found.group()), line)
        output_document.write(new_line)
    else:
        output_document.write(line)

So, what am I doing wrong? How do I get a function to work inside of a re.sub?

3
  • stylistically - please save CamelCase for classes and use words_separated_by_underscores for function names. (i.e. def feet_frames_to_time_code(feet_frames)). This makes it much clearer what is going on. (Notice how your question highlights the CamelCase differently then the rest of the code?) Commented Jun 7, 2013 at 3:19
  • I learned a new word today... CamelCase. As I said, I don't know much about programming... much less what a class is. I'm learning though and appreciate the input. Commented Jun 7, 2013 at 18:37
  • Well, if you learned it, may as well say that that’s PascalCase, not camelCase. =) Commented Jun 8, 2013 at 1:48

2 Answers 2

1

resub can take a second argument of a callable rather than just the replacement string.

If you pass a callable, it must take the match object.

You should also follow the python pep8 to make the python more readable (for other pythonistas)

Something like:

def feetframes_to_timecode(feetframes_match, framerate=24):
    feetframes = feetframes_match.group()
    frames = int(feetframes[:4])*16+int(feetframes[-2:])
    return "%02d:%02d:%02d:%02d" % (frames/(3600*framerate),
                                    frames/(60*framerate)%60,
                                    frames/framerate%60,
                                    frames%framerate)
# then just use:
for line in input_document:
    output_document.write(re.sub(r'\d{4}\+\d\d', feetframes_to_timecode, line))
Sign up to request clarification or add additional context in comments.

2 Comments

I apologize for my bad scripting as I have no formal education on this topic. I simply have an idea and Google my way to the final script. I will take a look at whatever pep8 is and try to improve my formatting. Thanks for all your help on this!
Ahhh! I see! Move the match.group() to the function instead! That's a good idea.
0

Your example code has an extra '(' that does not belong. This is what the line should be:

new_line = re.sub(r'\d{4}\+\d{2}', FeetFramesToTimecode(found.group()), line)

This produced output for me like you want.

1 Comment

Doh! I feel like Homer on this one. Thanks for pointing this out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.