Scripting way to upload videos to peertube and youtube
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
6.9 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. #!/usr/bin/python
  2. # coding: utf-8
  3. """
  4. prismedia_upload - tool to upload videos to Peertube and Youtube
  5. Usage:
  6. prismedia_upload.py --file=<FILE> [options]
  7. prismedia_upload.py --file=<FILE> --tags=STRING [--mt options]
  8. prismedia_upload.py -h | --help
  9. prismedia_upload.py --version
  10. Options:
  11. --name=NAME Name of the video to upload. (default to video filename)
  12. -d, --description=STRING Description of the video. (default: default description)
  13. -t, --tags=STRING Tags for the video. comma separated.
  14. WARN: tags with space and special characters (!, ', ", ?, ...)
  15. are not supported by Mastodon to be published from Peertube
  16. use mastodon compatibility below
  17. --mt Force Mastodon compatibility for tags (drop every incompatible characters inside tags)
  18. This option requires --tags
  19. -c, --category=STRING Category for the videos, see below. (default: Films)
  20. --cca License should be CreativeCommon Attribution (affects Youtube upload only)
  21. -p, --privacy=STRING Choose between public, unlisted or private. (default: private)
  22. --disable-comments Disable comments (Peertube only as YT API does not support) (default: comments are enabled)
  23. --nsfw Set the video as No Safe For Work (Peertube only as YT API does not support) (default: video is safe)
  24. --nfo=STRING Configure a specific nfo file to set options for the video.
  25. By default Prismedia search a .txt based on video name
  26. See nfo_example.txt for more details
  27. --platform=STRING List of platform(s) to upload to, comma separated.
  28. Supported platforms are youtube and peertube (default is both)
  29. --language=STRING Specify the default language for video. See below for supported language. (default is English)
  30. -h --help Show this help.
  31. --version Show version.
  32. Categories:
  33. Category is the type of video you upload. Default is films.
  34. Here are available categories from Peertube and Youtube:
  35. music, films, vehicles,
  36. sports, travels, gaming, people,
  37. comedy, entertainment, news,
  38. how to, education, activism, science & technology,
  39. science, technology, animals
  40. Languages:
  41. Language of the video (audio track), choose one. Default is English
  42. Here are available languages from Peertube and Youtube:
  43. Arabic, English, French, German, Hindi, Italian,
  44. Japanese, Korean, Mandarin, Portuguese, Punjabi, Russian, Spanish
  45. """
  46. from os.path import dirname, realpath
  47. import sys
  48. from docopt import docopt
  49. # Allows a relative import from the parent folder
  50. sys.path.insert(0, dirname(realpath(__file__)) + "/lib")
  51. import yt_upload
  52. import pt_upload
  53. import utils
  54. try:
  55. # noinspection PyUnresolvedReferences
  56. from schema import Schema, And, Or, Optional, SchemaError
  57. except ImportError:
  58. exit('This program requires that the `schema` data-validation library'
  59. ' is installed: \n'
  60. 'see https://github.com/halst/schema\n')
  61. try:
  62. # noinspection PyUnresolvedReferences
  63. import magic
  64. except ImportError:
  65. exit('This program requires that the `python-magic` library'
  66. ' is installed, NOT the Python bindings to libmagic API \n'
  67. 'see https://github.com/ahupp/python-magic\n')
  68. VERSION = "prismedia v0.3"
  69. VALID_PRIVACY_STATUSES = ('public', 'private', 'unlisted')
  70. VALID_CATEGORIES = (
  71. "music", "films", "vehicles",
  72. "sports", "travels", "gaming", "people",
  73. "comedy", "entertainment", "news",
  74. "how to", "education", "activism", "science & technology",
  75. "science", "technology", "animals"
  76. )
  77. VALID_PLATFORM = ('youtube', 'peertube')
  78. VALID_LANGUAGES = ('arabic', 'english', 'french',
  79. 'german', 'hindi', 'italian',
  80. 'japanese', 'korean', 'mandarin',
  81. 'portuguese', 'punjabi', 'russian', 'spanish')
  82. def validateVideo(path):
  83. supported_types = ['video/mp4']
  84. if magic.from_file(path, mime=True) in supported_types:
  85. return path
  86. else:
  87. return False
  88. def validateCategory(category):
  89. if category.lower() in VALID_CATEGORIES:
  90. return True
  91. else:
  92. return False
  93. def validatePrivacy(privacy):
  94. if privacy.lower() in VALID_PRIVACY_STATUSES:
  95. return True
  96. else:
  97. return False
  98. def validatePlatform(platform):
  99. for plfrm in platform.split(','):
  100. if plfrm.lower().replace(" ", "") not in VALID_PLATFORM:
  101. return False
  102. return True
  103. def validateLanguage(language):
  104. if language.lower() in VALID_LANGUAGES:
  105. return True
  106. else:
  107. return False
  108. if __name__ == '__main__':
  109. options = docopt(__doc__, version=VERSION)
  110. schema = Schema({
  111. '--file': And(str, validateVideo, error='file is not supported, please use mp4'),
  112. Optional('--name'): Or(None, And(
  113. str,
  114. lambda x: not x.isdigit(),
  115. error="The video name should be a string")
  116. ),
  117. Optional('--description'): Or(None, And(
  118. str,
  119. lambda x: not x.isdigit(),
  120. error="The video name should be a string")
  121. ),
  122. Optional('--tags'): Or(None, And(
  123. str,
  124. lambda x: not x.isdigit(),
  125. error="Tags should be a string")
  126. ),
  127. Optional('--mt'): bool,
  128. Optional('--category'): Or(None, And(
  129. str,
  130. validateCategory,
  131. error="Category not recognized, please see --help")
  132. ),
  133. Optional('--language'): Or(None, And(
  134. str,
  135. validateLanguage,
  136. error="Language not recognized, please see --help")
  137. ),
  138. Optional('--privacy'): Or(None, And(
  139. str,
  140. validatePrivacy,
  141. error="Please use recognized privacy between public, unlisted or private")
  142. ),
  143. Optional('--nfo'): Or(None, str),
  144. Optional('--platform'): Or(None, And(str, validatePlatform, error="Sorry, upload platform not supported")),
  145. Optional('--cca'): bool,
  146. Optional('--disable-comments'): bool,
  147. Optional('--nsfw'): bool,
  148. '--help': bool,
  149. '--version': bool
  150. })
  151. options = utils.parseNFO(options)
  152. try:
  153. options = schema.validate(options)
  154. except SchemaError as e:
  155. exit(e)
  156. if options.get('--platform') is None or "youtube" in options.get('--platform'):
  157. yt_upload.run(options)
  158. if options.get('--platform') is None or "peertube" in options.get('--platform'):
  159. pt_upload.run(options)