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.

230 lines
9.2 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
  1. #!/usr/bin/env python2
  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 -f <FILE> --tags=STRING [options]
  8. prismedia_upload.py -h | --help
  9. prismedia_upload.py --version
  10. Options:
  11. -f, --file=STRING Path to the video file to upload in mp4
  12. --name=NAME Name of the video to upload. (default to video filename)
  13. -d, --description=STRING Description of the video. (default: default description)
  14. -t, --tags=STRING Tags for the video. comma separated.
  15. WARN: tags with space and special characters (!, ', ", ?, ...)
  16. are not supported by Mastodon to be published from Peertube
  17. use mastodon compatibility below
  18. -c, --category=STRING Category for the videos, see below. (default: Films)
  19. --cca License should be CreativeCommon Attribution (affects Youtube upload only)
  20. -p, --privacy=STRING Choose between public, unlisted or private. (default: private)
  21. --disable-comments Disable comments (Peertube only as YT API does not support) (default: comments are enabled)
  22. --nsfw Set the video as No Safe For Work (Peertube only as YT API does not support) (default: video is safe)
  23. --nfo=STRING Configure a specific nfo file to set options for the video.
  24. By default Prismedia search a .txt based on video name
  25. See nfo_example.txt for more details
  26. --platform=STRING List of platform(s) to upload to, comma separated.
  27. Supported platforms are youtube and peertube (default is both)
  28. --language=STRING Specify the default language for video. See below for supported language. (default is English)
  29. --publishAt=DATE Publish the video at the given DATE using local server timezone.
  30. DATE should be on the form YYYY-MM-DDThh:mm:ss eg: 2018-03-12T19:00:00
  31. DATE should be in the future
  32. For Peertube, requires the "atd" and "curl utilities installed on the system
  33. --thumbnail=STRING Path to a file to use as a thumbnail for the video.
  34. Supported types are jpg and jpeg.
  35. By default, prismedia search for an image based on video name followed by .jpg or .jpeg
  36. --playlist=STRING Set the playlist to use for the video. Also known as Channel for Peertube.
  37. If the playlist is not found, spawn an error except if --playlist-create is set.
  38. --playlistCreate Create the playlist if not exists. (default do not create)
  39. Only relevant if --playlist is set.
  40. -h --help Show this help.
  41. --version Show version.
  42. Categories:
  43. Category is the type of video you upload. Default is films.
  44. Here are available categories from Peertube and Youtube:
  45. music, films, vehicles,
  46. sports, travels, gaming, people,
  47. comedy, entertainment, news,
  48. how to, education, activism, science & technology,
  49. science, technology, animals
  50. Languages:
  51. Language of the video (audio track), choose one. Default is English
  52. Here are available languages from Peertube and Youtube:
  53. Arabic, English, French, German, Hindi, Italian,
  54. Japanese, Korean, Mandarin, Portuguese, Punjabi, Russian, Spanish
  55. """
  56. from os.path import dirname, realpath
  57. import sys
  58. import datetime
  59. import locale
  60. import logging
  61. logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
  62. from docopt import docopt
  63. # Allows a relative import from the parent folder
  64. sys.path.insert(0, dirname(realpath(__file__)) + "/lib")
  65. import yt_upload
  66. import pt_upload
  67. import utils
  68. try:
  69. # noinspection PyUnresolvedReferences
  70. from schema import Schema, And, Or, Optional, SchemaError
  71. except ImportError:
  72. logging.error('This program requires that the `schema` data-validation library'
  73. ' is installed: \n'
  74. 'see https://github.com/halst/schema\n')
  75. exit(1)
  76. try:
  77. # noinspection PyUnresolvedReferences
  78. import magic
  79. except ImportError:
  80. logging.error('This program requires that the `python-magic` library'
  81. ' is installed, NOT the Python bindings to libmagic API \n'
  82. 'see https://github.com/ahupp/python-magic\n')
  83. exit(1)
  84. VERSION = "prismedia v0.6.4"
  85. VALID_PRIVACY_STATUSES = ('public', 'private', 'unlisted')
  86. VALID_CATEGORIES = (
  87. "music", "films", "vehicles",
  88. "sports", "travels", "gaming", "people",
  89. "comedy", "entertainment", "news",
  90. "how to", "education", "activism", "science & technology",
  91. "science", "technology", "animals"
  92. )
  93. VALID_PLATFORM = ('youtube', 'peertube')
  94. VALID_LANGUAGES = ('arabic', 'english', 'french',
  95. 'german', 'hindi', 'italian',
  96. 'japanese', 'korean', 'mandarin',
  97. 'portuguese', 'punjabi', 'russian', 'spanish')
  98. def validateVideo(path):
  99. supported_types = ['video/mp4']
  100. if magic.from_file(path, mime=True) in supported_types:
  101. return path
  102. else:
  103. return False
  104. def validateCategory(category):
  105. if category.lower() in VALID_CATEGORIES:
  106. return True
  107. else:
  108. return False
  109. def validatePrivacy(privacy):
  110. if privacy.lower() in VALID_PRIVACY_STATUSES:
  111. return True
  112. else:
  113. return False
  114. def validatePlatform(platform):
  115. for plfrm in platform.split(','):
  116. if plfrm.lower().replace(" ", "") not in VALID_PLATFORM:
  117. return False
  118. return True
  119. def validateLanguage(language):
  120. if language.lower() in VALID_LANGUAGES:
  121. return True
  122. else:
  123. return False
  124. def validatePublish(publish):
  125. # Check date format and if date is future
  126. try:
  127. now = datetime.datetime.now()
  128. publishAt = datetime.datetime.strptime(publish, '%Y-%m-%dT%H:%M:%S')
  129. if now >= publishAt:
  130. return False
  131. except ValueError:
  132. return False
  133. return True
  134. def validateThumbnail(thumbnail):
  135. supported_types = ['image/jpg', 'image/jpeg']
  136. if magic.from_file(thumbnail, mime=True) in supported_types:
  137. return thumbnail
  138. else:
  139. return False
  140. if __name__ == '__main__':
  141. options = docopt(__doc__, version=VERSION)
  142. schema = Schema({
  143. '--file': And(str, validateVideo, error='file is not supported, please use mp4'),
  144. Optional('--name'): Or(None, And(
  145. basestring,
  146. lambda x: not x.isdigit(),
  147. error="The video name should be a string")
  148. ),
  149. Optional('--description'): Or(None, And(
  150. basestring,
  151. lambda x: not x.isdigit(),
  152. error="The video description should be a string")
  153. ),
  154. Optional('--tags'): Or(None, And(
  155. basestring,
  156. lambda x: not x.isdigit(),
  157. error="Tags should be a string")
  158. ),
  159. Optional('--category'): Or(None, And(
  160. str,
  161. validateCategory,
  162. error="Category not recognized, please see --help")
  163. ),
  164. Optional('--language'): Or(None, And(
  165. str,
  166. validateLanguage,
  167. error="Language not recognized, please see --help")
  168. ),
  169. Optional('--privacy'): Or(None, And(
  170. str,
  171. validatePrivacy,
  172. error="Please use recognized privacy between public, unlisted or private")
  173. ),
  174. Optional('--nfo'): Or(None, str),
  175. Optional('--platform'): Or(None, And(str, validatePlatform, error="Sorry, upload platform not supported")),
  176. Optional('--publishAt'): Or(None, And(
  177. str,
  178. validatePublish,
  179. error="DATE should be the form YYYY-MM-DDThh:mm:ss and has to be in the future")
  180. ),
  181. Optional('--cca'): bool,
  182. Optional('--disable-comments'): bool,
  183. Optional('--nsfw'): bool,
  184. Optional('--thumbnail'): Or(None, And(
  185. str, validateThumbnail, error='thumbnail is not supported, please use jpg/jpeg'),
  186. ),
  187. Optional('--playlist'): Or(None, str),
  188. Optional('--playlistCreate'): bool,
  189. '--help': bool,
  190. '--version': bool
  191. })
  192. utils.decodeArgumentStrings(options, locale.getpreferredencoding())
  193. options = utils.parseNFO(options)
  194. if not options.get('--thumbnail'):
  195. options = utils.searchThumbnail(options)
  196. try:
  197. options = schema.validate(options)
  198. except SchemaError as e:
  199. exit(e)
  200. if options.get('--platform') is None or "youtube" in options.get('--platform'):
  201. yt_upload.run(options)
  202. if options.get('--platform') is None or "peertube" in options.get('--platform'):
  203. pt_upload.run(options)