import re
import string


def clean_filename(s):
    """Sanitize a string to be used as a filename."""
    # strip paren portions which contain trailing time length (...)
    s = re.sub("\([^\(]*$", "", s)
    s = s.strip().replace(':', '-').replace(' ', '_')
    valid_chars = "-_.()%s%s" % (string.ascii_letters, string.digits)
    return ''.join(c for c in s if c in valid_chars)