-
Notifications
You must be signed in to change notification settings - Fork 17
[BD-27] Upload transcripts downloaded by the video_download script #61
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
Changes from all commits
2b7b03d
225557d
47fec5d
45483c8
87f65ee
a5a041e
8489087
e770f1f
7a20adf
a3cdb57
c663606
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,9 +4,9 @@ Tools | |
| .. _video_upload_tool: | ||
|
|
||
| Video Upload Tool | ||
| ----------------- | ||
| ================= | ||
|
|
||
| The video upload tool uploads video files to edX's video encoding pipeline. | ||
| The video upload tool uploads video files and associated transcripts to edX's video encoding pipeline. | ||
| It also generates an output CSV that can be used as input to the video xBlock conversion tool. | ||
|
|
||
| Input | ||
|
|
@@ -16,8 +16,11 @@ The tool also optionally takes as a keyword argument an ``--output-csv``. | |
|
|
||
| The ``course-id`` argument is the ID of the course as it appears in Studio. For example, ``course-v1:edX+111222+111222``. | ||
|
|
||
| The ``directory`` argument is a directory containing video files that will be uploaded to edX's video encoding pipeline. | ||
| The ``directory`` argument is a directory (such as the one created by the video download tool) containing video files and transcripts, that will be uploaded to edX's video encoding pipeline. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How should this directory be structured? Will all of the video files and transcripts be within the same directory?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added some clarification to it's file searching behavior in this section. Does that help on this point? |
||
| The currently supported formats for the videos are ``mp4`` and ``mov``, which are the files supported by the edX video encoding pipeline. | ||
| The tool will recurse into the provided directory and files at any depth, and matching the supported formats will be uploaded. | ||
| The only supported format for transcripts is ``srt``, and transcript files must have the same location and base name as their associated video, apart from the suffix. | ||
| For example for a video file named video_name.mp4 the tool will find transcripts in the same directory, named like video_name.XX.srt where XX is a language code. | ||
| Note that files of other types will be ignored by the tool. | ||
|
|
||
| The ``input-csv`` argument is a file that is meant to establish a relationship between a particular video, its externally hosted URL, and the edX video ID that is generated by the video encoding pipeline when the video is uploaded. | ||
|
|
@@ -36,7 +39,10 @@ Output | |
| ------ | ||
| The tool will generate a CSV file. If the optional command line argument ``--output-csv`` is supplied, then the output CSV will be saved to that path. Otherwise, if the command line argument is not supplied, then the file will be saved to the same directory as the input file as specified by the ``input-csv`` command line argument, and the name of the CSV fill will be the name of the original CSV file supplied as a command line argument with "upload-results" appended to the end of the name, i.e. ``<original-filename>-upload-results.csv``. | ||
|
|
||
| A new column, "Edx Id", which represents the edX Video ID for each video, will be appended to the end of the file. | ||
| Two new columns will be appended to the end of the file: | ||
| * "Edx Id", which represents the edX Video ID for each video | ||
| * "Languages", a list of dash separated, alphabetically ordered language codes for which transcripts were uploaded for each video. | ||
|
|
||
| Otherwise, the file is identical to the file supplied as a command line argument. | ||
|
|
||
| Logs | ||
|
|
@@ -72,3 +78,38 @@ Once these are done, you can run the tool as follows:: | |
| Or, if you would like to specify the path to the output CSV file:: | ||
| python src/cc2olx/tools/video_upload.py course-v1:edX+111222+111222 /Users/example/workspaces/videos /Users/example/workspaces/video-data.csv --output-csv /Users/example/workspaces/video-data-output.csv | ||
|
|
||
| .. _video_download_tool: | ||
|
|
||
| Video Download Tool | ||
| =================== | ||
| The download tool will accept a common cartridge format course, and search for matching videos to download, in preparation for use with the video upload tool. | ||
| It also generates a CSV in a format that can be used by the video upload tool for inclusion in the edX video pipeline (by default "out.csv"). | ||
|
|
||
| Input | ||
| ----- | ||
| The tool has one required parameter ``-i`` or ``--input``, which is the Common Cartridge Course (IMSCC) format archive, or single HTML file. This is searched for iframes with video links. | ||
| The tool will download the raw video for any embedded video links, as well as associated transcripts, and store these in the `downloads` folder in the current working directory. | ||
|
|
||
| The ``--output`` argument allows for specifying an alternative filename to store the CSV metadata generated for downloaded videos. | ||
|
|
||
| The ``--downloads`` argument will specify an alternative directory for storing downloaded video data. | ||
|
|
||
| The ``--simulate`` argument allows for a dry-run of parsing and extracting URLs for download, without actually downloading the video content. | ||
|
|
||
| The ``--config`` argument allows for providing extra configuration to youtube-dl which is used to handle video downloading. | ||
|
|
||
| Output | ||
| ------ | ||
| Unless otherwise specified with the options above, the tool will generate a file ``out.csv`` containing the URL to the video, file path of the downloaded video, and a YouTube ID if the video was originally hosted on YouTube. | ||
| The tool will also create a `downloads` directory with the raw videos downloaded, and any transcripts or subtitles associated with the videos. | ||
|
|
||
| Use | ||
| --- | ||
| The video download tool is a basic command-line Python 3 program. | ||
| This can be run simply against a Common Cartridge Format Course (IMSCC) file as so:: | ||
|
|
||
| python src/cc2olx/tools/video_download.py -i ~/ushistory.imscc | ||
|
|
||
| This tool is most useful by chaining it with the video upload tool, in which case the video upload tool can be run in the same directory after download is complete like so:: | ||
|
|
||
| python src/cc2olx/tools/video_upload.py course-v1:edX+111222+111222 ./downloads ./out.csv | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,8 +8,11 @@ | |
|
|
||
| OAUTH_TOKEN_URL = "https://courses.edx.org/oauth2/access_token" | ||
| GENERATE_UPLOAD_LINK_BASE_URL = "https://studio.edx.org/generate_video_upload_link/" | ||
| TRANSCRIPT_UPLOAD_LINK = "https://studio.edx.org/transcript_upload_api/" | ||
| # OAUTH_TOKEN_URL = "https://courses.stage.edx.org/oauth2/access_token" | ||
| # GENERATE_UPLOAD_LINK_BASE_URL = "https://studio.stage.edx.org/generate_video_upload_link/" | ||
| # TRANSCRIPT_UPLOAD_LINK = "https://studio.stage.edx.org/transcript_upload_api/" | ||
|
|
||
| VIDEO_EXTENSION_CONTENT_TYPES = { | ||
| ".mp4": "video/mp4", | ||
| ".mov": "video/quicktime", | ||
|
|
@@ -55,7 +58,7 @@ def get_access_token(): | |
| return data["access_token"] | ||
|
|
||
|
|
||
| def parse_args(): | ||
| def parse_args(args=None): | ||
| """Set up and return command line arguments for the video upload tool.""" | ||
| parser = argparse.ArgumentParser(description="Upload video files to edX via Studio's video encoding pipeline.") | ||
| parser.add_argument( | ||
|
|
@@ -74,9 +77,10 @@ def parse_args(): | |
| ) | ||
| parser.add_argument( | ||
| "--output-csv", | ||
| "-o", | ||
| help="path to where the output CSV should be stored; this will overwrite existing files", | ||
| ) | ||
| return parser.parse_args() | ||
| return parser.parse_args(args) | ||
|
|
||
|
|
||
| def make_generate_upload_link_request(url, data, filename, access_token): | ||
|
|
@@ -123,6 +127,42 @@ def make_generate_upload_link_request(url, data, filename, access_token): | |
| return response | ||
|
|
||
|
|
||
| def upload_transcript(filename, edx_video_id, language_code, access_token): | ||
| """ | ||
| Make a POST request against the Studio upload transcript API and return the | ||
| response. If errors occur during the API call, log to the console. | ||
|
|
||
| Arguments: | ||
| * filename: the transcript filename | ||
| * edx_video_id: the video ID of the video this transcript is for | ||
| * language_code: the language of the transcript | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add access token to docstring args
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for catching that. It's added now. |
||
| * access_token: access token to be able to make authenticated calls to the Studio API | ||
|
|
||
| Returns: | ||
| * response: the response object from the POST API call | ||
| """ | ||
| s = requests.Session() | ||
| s.auth = SuppliedJwtAuth(access_token) | ||
|
|
||
| data = {"edx_video_id": edx_video_id, "language_code": language_code, "new_language_code": language_code} | ||
| files = {"file": open(filename, "rb")} | ||
|
|
||
| try: | ||
| response = s.post(TRANSCRIPT_UPLOAD_LINK, data=data, files=files) | ||
| response.raise_for_status() | ||
| except requests.exceptions.HTTPError as error: | ||
| print( | ||
| "An HTTP error occurred calling the Studio transcript upload link API " | ||
| "for transcript: {}: {}".format(filename, repr(error)) | ||
| ) | ||
| if response.status_code == 201: | ||
| print(f"Successfully uploaded transcript {filename}.") | ||
| else: | ||
| print(f"Transcript {filename} was unable to be uploaded.") | ||
|
|
||
| return response | ||
|
|
||
|
|
||
| def make_upload_video_request(url, data, headers, filename): | ||
| """ | ||
| Make a PUT request against the AWS upload video API. | ||
|
|
@@ -185,6 +225,7 @@ def write_upload_results_csv(input_csv_path, output_csv_path, file_data): | |
|
|
||
| new_fieldnames = reader.fieldnames.copy() | ||
| new_fieldnames.append("Edx Id") | ||
| new_fieldnames.append("Languages") | ||
|
|
||
| writer = csv.DictWriter(output_csv, new_fieldnames) | ||
| writer.writeheader() | ||
|
|
@@ -199,6 +240,7 @@ def write_upload_results_csv(input_csv_path, output_csv_path, file_data): | |
|
|
||
| new_row = row.copy() | ||
| new_row["Edx Id"] = data["edx_video_id"] | ||
| new_row["Languages"] = data["lang"] | ||
|
|
||
| writer.writerow(new_row) | ||
|
|
||
|
|
@@ -260,6 +302,15 @@ def main(): | |
| make_upload_video_request(upload_url, data, headers, filename) | ||
|
|
||
| files_data[str(relative_path)] = {"edx_video_id": edx_video_id} | ||
| langs = [] | ||
|
|
||
| # Look for files with the same name as our video but with a ${LANG}.srt suffix | ||
| for srt_path in sorted(full_path.parent.glob(full_path.stem + "*.srt")): | ||
| lang = srt_path.suffixes[0][1:] | ||
| langs.append(lang) | ||
| upload_transcript(srt_path, edx_video_id, lang, access_token) | ||
|
|
||
| files_data[str(relative_path)]["lang"] = "-".join(langs) | ||
|
|
||
| input_csv_path = Path(args.input_csv) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kaizoku can we also include documentation for the video download tool? It can be added to this file, but maybe under another section
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I added a section below describing the options to the video download tool, its expected input and output, and how it can be used with the video upload tool. Does this extra section look good?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That looks great, thanks for adding!