Exemplo n.º 1
0
def download_video(id):
    logger.info("Video(%s)" % (id, ))
    try:
        # get video object from ID
        v = Video(id=id,
                  _connection=OLDCONN,
                  media_delivery="http",
                  custom_fields=MY_CUSTOM_FIELDS)
    except NoDataFoundError:
        logger.warn("  **> No data found for (%s)" % (id, ))
        return None

    # determine best rendition (largest size)
    best_rendition = None
    for rendition in v.renditions:
        if rendition.url is not None and rendition.url.startswith('http'):
            if best_rendition is None:
                best_rendition = rendition
            elif rendition.size > best_rendition.size:
                best_rendition = rendition

    if best_rendition is None:
        logger.warn("  No best rendition found for (%s). Skipping." % (id, ))
        return
    else:
        # use best rendition to fetch video
        fname = DOWNLOAD_DIR + str(id)
        if best_rendition.video_codec == 'H264':
            fname += ".mp4"
        elif best_rendition.video_codec == 'M2TS':
            fname += ".m2ts"
        logger.info("  fetching ID(%s) codec(%s) to (%s)" % (
            v.id,
            best_rendition.video_codec,
            fname,
        ))

        try:
            req = urllib2.urlopen(best_rendition.url)
            downloaded = 0
            CHUNK = 4096
            with open(fname, 'wb') as fp:
                while True:
                    chunk = req.read(CHUNK)
                    downloaded += len(chunk)
                    if not chunk: break
                    fp.write(chunk)
            logger.debug("  bytes downloaded: " + str(downloaded))
            v._filename = fname  # required for upload with v.save()
        except urllib2.HTTPError, e:
            logger.warn("HTTP Error: %s - (%s)" % (
                e.code,
                url,
            ))
            return False
        except urllib2.URLError, e:
            logger.warn("URL Error: %s - (%s)" % (
                e.reason,
                url,
            ))
            return False
Exemplo n.º 2
0
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from pybrightcove.connection import Connection
from pybrightcove.video import Video

c = Connection()

import sys

video = Video()
video.name = sys.argv[1]
video.shortDescription = sys.argv[1]*50  

for tag in sys.argv[2:]:
    video.tags.append(tag)

print "Attempting to upload %s" % sys.argv[1]
c.create_video(sys.argv[1], video=video)
print "Done"