Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate only small sized videos #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 72 additions & 1 deletion migrate-events-to-another-opencast/migrate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env python3

import json

import lxml.etree as le
import urllib3

SOURCE_HOST_ADMIN = 'https://source-admin.opencast.org'
Expand All @@ -13,6 +15,8 @@
TARGET_PASS = 'opencast'
TARGET_WORKFLOW = 'import'

ONLY_SMALL_VIDEO_TRACKS = False


http = urllib3.PoolManager()

Expand Down Expand Up @@ -82,6 +86,61 @@ def create_series(dublincore, acl):
print('Create series response:', request.status)


def get_smallest_video_track_by_type(tracks):
'''
Searches the smallest video track for each track type. Tracks which do not contain a video will be ignored.

:param tracks: List of tracks
:return: Dictionary with keys for the type and values containing the smallest video track
'''
# Filter only video tracks
# Ignore videos with mimetype = application/x-mpegURL as these can not always be processed by opencast
tracks = filter(lambda track: 'video' in track and 'video' in track.get('mimetype'), tracks)

smallest_video_tracks = {}
for track in tracks:
track_type = track.get('type')
size = track.get('size')

if track_type not in smallest_video_tracks or size < smallest_video_tracks[track_type].get('size'):
smallest_video_tracks[track_type] = track

return smallest_video_tracks


def remove_large_video_tracks_from_mediapackage(mediapackage, tracks):
'''
For each track type, removes all video tracks that are bigger than the smallest video track

:param mediapackage: Media package as XML
:param tracks: tracks as list used to find the smallest track
:return: filtered media package as XML
'''
smallest_video_tracks = get_smallest_video_track_by_type(tracks)

mediapackage = le.fromstring(bytes(mediapackage, encoding='utf-8'))
media = mediapackage.find('{*}media')
tracks = media.findall('{*}track')

for track in tracks:
track_id = track.get('id')
track_type = track.get('type')
video = track.find('{*}video')
mimetype = track.find('{*}mimetype')

# Remove useless hls playlists
if mimetype.text == 'application/x-mpegURL':
media.remove(track)

# Ensure video exists, otherwise do not remove
elif video is not None and 'video' in mimetype.text:
# Remove bigger video track
if track_type in smallest_video_tracks and smallest_video_tracks[track_type].get('id') != track_id:
media.remove(track)

return le.tostring(mediapackage, encoding='utf-8')


def get_published_media():
'''
Generator, requesting all published media packages from the search service
Expand All @@ -108,12 +167,24 @@ def get_published_media():
offset = data.get('offset') + limit
total = data.get('total')
results = data.get('result', [])

if type(results) is not list:
results = [results]

for result in results:
print('Importing ' + result.get('id'))
yield result.get('ocMediapackage')

if ONLY_SMALL_VIDEO_TRACKS:
media_package = result.get('mediapackage', {})
media = media_package.get('media', {})
tracks = media.get('track', [])

if type(tracks) is not list:
tracks = [tracks]

yield remove_large_video_tracks_from_mediapackage(result.get('ocMediapackage'), tracks)
else:
yield result.get('ocMediapackage')


def ingest(mediapackage):
Expand Down
2 changes: 1 addition & 1 deletion migrate-events-to-another-opencast/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ You can use any workflow you want and even reprocess videos,
but if you just want to import and publish media as they were in the old system,
you may want to use the workflow [import.yaml](import.yaml).

Update the credentials and the workflow in the `migrate.py` to configure your Opencast source and target systems. If you have a distributed installation of Opencast ensure that the URLs match your admin and presentation node. With an AllInOne installation you'll have the same URL for admin and presentation.
Update the credentials and the workflow in the `migrate.py` to configure your Opencast source and target systems. If you have a distributed installation of Opencast ensure that the URLs match your admin and presentation node. With an AllInOne installation you'll have the same URL for admin and presentation. You can configure with `ONLY_SMALL_VIDEO_TRACKS` whether only the smallest video with a low resolution should be migrated.

Finally, start the migration:

Expand Down