FFMpeg sample

You can use the ffmpeg sample to encode or transcode your video. The command line to use is the same than ffmpeg. The input file option -i is detected and parsed so your source video is automatically added and uploaded to a bucket. The results are also downloaded automatically once the processing is complete.

To run the following sample rename samples.conf.example to samples.conf and edit with your own authentication token.

  • samples.conf

# This is your secret token, keep it safe
  • media/qarnot_computing.mp4
mkdir media
wget -O ./media/qarnot_computing.mp4
#!/usr/bin/env python
import argparse
import sys
import qarnot

# Parse ffmpeg command line

# Create a argument parser to parse input files
parser = argparse.ArgumentParser()
parser.add_argument('-i', action='append', required=True)

# Get arguments
args = sys.argv[1:]

# Parse input files and store them as a list
input_files = parser.parse_known_args(args)[0].i

# Build the full command line
ffmpeg_cmd = ' '.join(args)

# Display that we parsed
print("** FFMPEG command: %s" % ffmpeg_cmd)
print("** Input files: %s" % ', '.join(input_files))

# Edit 'samples.conf' to provide your own credentials

# Create a connection, from which all other objects will be derived
conn = qarnot.Connection('samples.conf')

# Create a task. The 'with' statement ensures that the task will be
# deleted in the end, to prevent tasks from continuing to run after
# a Ctrl-C for instance
task = conn.create_task('sample4-ffmpeg', 'docker-batch', 1)

# Store if an error happened during the process
error_happened = False

    # Set the command to run when launching the container, by overriding a
    # constant.
    # Task constants are the main way of controlling a task's behaviour
    task.constants['DOCKER_REPO'] = 'jrottenberg/ffmpeg'
    task.constants['DOCKER_TAG'] = 'ubuntu'
    task.constants['DOCKER_CMD'] = ffmpeg_cmd
    # task.constants['DOCKER_CMD'] = "sleep 3600"

    input_bucket = conn.create_bucket('sample4-ffmpeg-input-resource')
    for input_file in input_files:
        # Create a resource bucket and add our input file.
        input_bucket[input_file] = input_file

    # Attach the bucket to the task

    # Create and attach an output bucket
    output_bucket = conn.create_bucket('sample4-ffmpeg-output')
    task.results = output_bucket

    # Submit the task to the Api, that will launch it on the cluster

    # Wait for the task to be finished, and monitor the progress of its
    # deployment
    last_state = ''
    done = False
    while not done:
        if task.state != last_state:
            last_state = task.state
            print("** {}".format(last_state))

        # Wait for the task to complete, with a timeout of 5 seconds.
        # This will return True as soon as the task is complete, or False
        # after the timeout.
        done = task.wait(5)

        # Display fresh stdout / stderr

    if task.state == 'Failure':
        # Display errors on failure
        print("** Errors: %s" % task.errors[0])
        error_happened = True
        # Or download the results

    task.delete(purge_resources=True, purge_results=True)
    # Exit code in case of error
    if error_happened:
  • Run the sample
python -i media/qarnot_computing.mp4 media/qarnot_computing.mpg

This sample is available on Github.

Next steps

  • Discover a Qarnot built-in application with the Blender sample.