# Path: utils/mem.py
# def get_free_space_bytes(path="/tmp"):
#     cmd = "/bin/df %s --output=avail | tail -n 1" %path
#     return int(get_stdout(cmd, shell=True))*1024
# 
# Path: utils/files.py
# def check_temp_path(path='/tmp'):
#     path = os.path.dirname(path)
#     if not check_path_is_tmpfs(path):
#         print('Path %s is not a tmpfs' % path)
#         return False
#     if not check_path_is_writable(path):
#         print('Path %s is not writable' % path)
#         return False
#     return True
# 
# Path: utils/process.py
# def run_cmd(cmd, shell=False):
#     env = dict(os.environ)
#     env["LANG"] = "C"
#     if shell:
#         args = cmd
#     else:
#         args = shlex.split(cmd)
#     p = subprocess.Popen(args, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, shell=shell)
#     stdout, stderr = p.communicate()
#     rc = p.returncode
#     if rc != 0:
#         if '|' in cmd:
#             raise Exception('Command %s failed, maybe shell=True is required' %cmd)
#         else:
#             raise Exception('Command %s failed, error: %s' % (cmd, stderr))
#     elif contains_errors(stderr):
#         raise Exception('Command %s had an error' %cmd)
#     return rc, stdout, stderr


import os
from utils.mem import get_free_space_bytes
from utils.files import check_temp_path
from utils.process import run_cmd
        'colorspace': colorspace,
        'framerate': framerate,
        'raw_buf_file': raw_buf_file,
        'location': location,
        'blocksize': bufsize,
        'tmp_location': tmp_location,
        'tmp_num_buffers': tmp_num_buffers,
    }
    # vaapi decoder sometimes decodes 1080p as 1920x1088 frames, hence the videoscale
    # add videoconvert to fix https://bugzilla.gnome.org/show_bug.cgi?id=772457
    #pattern_gen_buf = "gst-launch-1.0 filesrc location={location} num-buffers={tmp_num_buffers} blocksize={blocksize} ! decodebin ! videoscale ! %s ! filesink location={tmp_location}" % pattern_caps
    pattern_gen_buf = "gst-launch-1.0 filesrc location={location} num-buffers={tmp_num_buffers} ! decodebin ! videoconvert ! videoscale ! %s ! filesink location={tmp_location}" % pattern_caps
    cmd = pattern_gen_buf.format(**format_dict)
    run_cmd(cmd)
    tmp_size = os.path.getsize(tmp_location)

    if not num_buffers:
        num_buffers = format_dict['num_buffers'] = min(get_num_buffers_for_path(colorspace, width, height, raw_buf_file, max_buffers), int(tmp_size/bufsize))
        print('Generate %s buffers (%ss)' % (num_buffers, int(round(num_buffers/framerate))))
    pattern_gen_buf = "gst-launch-1.0 filesrc location={tmp_location} num-buffers={num_buffers} blocksize={blocksize} ! filesink location=%s" % raw_buf_file
    cmd = pattern_gen_buf.format(**format_dict)
    print('Generate final sample')
    run_cmd(cmd)
    #caps = pattern_caps.format(**format_dict) 
    caps = "rawvideoparse width=%s height=%s framerate=%s format=%s" %(width, height, framerate, colorspace.lower())
    if os.path.isfile(tmp_location):
        os.remove(tmp_location)
    return num_buffers, bufsize, caps

def generate_buffers_from_pattern(colorspace, width, height, raw_buf_file=RAW_BUF_FILE, pattern='black', num_buffers=None, framerate=30, max_buffers=1000):

# Path: utils/mem.py
# def get_free_space_bytes(path="/tmp"):
#     cmd = "/bin/df %s --output=avail | tail -n 1" %path
#     return int(get_stdout(cmd, shell=True))*1024
# 
# Path: utils/files.py
# def check_temp_path(path='/tmp'):
#     path = os.path.dirname(path)
#     if not check_path_is_tmpfs(path):
#         print('Path %s is not a tmpfs' % path)
#         return False
#     if not check_path_is_writable(path):
#         print('Path %s is not writable' % path)
#         return False
#     return True
# 
# Path: utils/process.py
# def run_cmd(cmd, shell=False):
#     env = dict(os.environ)
#     env["LANG"] = "C"
#     if shell:
#         args = cmd
#     else:
#         args = shlex.split(cmd)
#     p = subprocess.Popen(args, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, shell=shell)
#     stdout, stderr = p.communicate()
#     rc = p.returncode
#     if rc != 0:
#         if '|' in cmd:
#             raise Exception('Command %s failed, maybe shell=True is required' %cmd)
#         else:
#             raise Exception('Command %s failed, error: %s' % (cmd, stderr))
#     elif contains_errors(stderr):
#         raise Exception('Command %s had an error' %cmd)
#     return rc, stdout, stderr


# Path: utils/video.py
import os
from utils.mem import get_free_space_bytes
from utils.files import check_temp_path
from utils.process import run_cmd

        'colorspace': colorspace,
        'framerate': framerate,
        'raw_buf_file': raw_buf_file,
        'location': location,
        'blocksize': bufsize,
        'tmp_location': tmp_location,
        'tmp_num_buffers': tmp_num_buffers,
    }
    # vaapi decoder sometimes decodes 1080p as 1920x1088 frames, hence the videoscale
    # add videoconvert to fix https://bugzilla.gnome.org/show_bug.cgi?id=772457
    #pattern_gen_buf = "gst-launch-1.0 filesrc location={location} num-buffers={tmp_num_buffers} blocksize={blocksize} ! decodebin ! videoscale ! %s ! filesink location={tmp_location}" % pattern_caps
    pattern_gen_buf = "gst-launch-1.0 filesrc location={location} num-buffers={tmp_num_buffers} ! decodebin ! videoconvert ! videoscale ! %s ! filesink location={tmp_location}" % pattern_caps
    cmd = pattern_gen_buf.format(**format_dict)
    run_cmd(cmd)
    tmp_size = os.path.getsize(tmp_location)

    if not num_buffers:
        num_buffers = format_dict['num_buffers'] = min(get_num_buffers_for_path(colorspace, width, height, raw_buf_file, max_buffers), int(tmp_size/bufsize))
        print('Generate %s buffers (%ss)' % (num_buffers, int(round(num_buffers/framerate))))
    pattern_gen_buf = "gst-launch-1.0 filesrc location={tmp_location} num-buffers={num_buffers} blocksize={blocksize} ! filesink location=%s" % raw_buf_file
    cmd = pattern_gen_buf.format(**format_dict)
    print('Generate final sample')
    run_cmd(cmd)
    #caps = pattern_caps.format(**format_dict) 
    caps = "rawvideoparse width=%s height=%s framerate=%s format=%s" %(width, height, framerate, colorspace.lower())
    if os.path.isfile(tmp_location):
        os.remove(tmp_location)
    return num_buffers, bufsize, caps

def generate_buffers_from_pattern(colorspace, width, height, raw_buf_file=RAW_BUF_FILE, pattern='black', num_buffers=None, framerate=30, max_buffers=1000):

    if not check_temp_path(raw_buf_file):