Created
September 25, 2014 04:38
-
-
Save hiszpanski/148889bfbbad90c6fa15 to your computer and use it in GitHub Desktop.
Transcoding boto example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # | |
| # transcoder.py | |
| # | |
| # Transcodes .ts into .mp4 | |
| # | |
| # Copyright 2014 Kuna Systems Corporation. All rights reserved. | |
| # | |
| import json | |
| import os | |
| import re | |
| import subprocess | |
| import tempfile | |
| import boto | |
| # | |
| username = 'xxxxxxxxx' | |
| password = 'xxxxxxxxx' | |
| # Get handle for connection to S3 | |
| s3 = boto.connect_s3() | |
| # Get handle for source bucket | |
| src_bucket = s3.get_bucket('xxxxxxxxxxx-rec') | |
| # Get handle for destination bucket | |
| dst_bucket = s3.get_bucket('xxxxxxxxxxx-trainingset') | |
| # Get iterator for bucket's keys | |
| keys = src_bucket.list() | |
| # Transcode each video | |
| for in_k in keys: | |
| out_key = in_k.key + '.mp4' | |
| # Only transcode files without a suffix | |
| if not re.search('\.', in_k.key): | |
| name = in_k.key.split('/')[0] | |
| url = 'test' | |
| s3_key = out_key | |
| data = json.dumps({'camera': { 'name': name }, 'url': url, 's3_key': s3_key}) | |
| # Skip if already exists | |
| if dst_bucket.get_key(out_key): | |
| print 'Skipping', in_k.key | |
| subprocess.check_call(["curl", "--dump-header", "-", | |
| "-H", "Content-Type: application/json", | |
| "-H", "Authorization: ApiKey xxxxxxxxxx:xxxxxxxxxxxxxxx", | |
| "-X", "POST", | |
| "--data", data, | |
| "http://localhost:8000/api/videotagger/video/"]) | |
| continue | |
| continue | |
| print 'Transcoding', in_k.key | |
| # Create temporary filename | |
| src = tempfile.mktemp(suffix='.ts') | |
| dst = tempfile.mktemp(suffix='.mp4') | |
| # Get key into temporary file | |
| in_k.get_contents_to_filename(src) | |
| try: | |
| # Transcode container | |
| subprocess.check_call(["ffmpeg", "-i", src, | |
| "-codec", "copy", "-an", dst]) | |
| # Put local transcoded output into output bucket | |
| out_k = boto.s3.key.Key(dst_bucket) | |
| out_k.key = out_key | |
| out_k.set_contents_from_filename(dst) | |
| except subprocess.CalledProcessError: | |
| print 'Failed to transcode', src, 'into', dst | |
| # Delete temporary files | |
| try: | |
| os.remove(src) | |
| except os.error: | |
| pass | |
| try: | |
| os.remove(dst) | |
| except os.error: | |
| pass | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment