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.

235 lines
9.1 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/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 logging
  60. logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
  61. from docopt import docopt
  62. # Allows a relative import from the parent folder
  63. sys.path.insert(0, dirname(realpath(__file__)) + "/lib")
  64. import yt_upload
  65. import pt_upload
  66. import utils
  67. try:
  68. # noinspection PyUnresolvedReferences
  69. from schema import Schema, And, Or, Optional, SchemaError
  70. except ImportError:
  71. logging.error('This program requires that the `schema` data-validation library'
  72. ' is installed: \n'
  73. 'see https://github.com/halst/schema\n')
  74. exit(1)
  75. try:
  76. # noinspection PyUnresolvedReferences
  77. import magic
  78. except ImportError:
  79. logging.error('This program requires that the `python-magic` library'
  80. ' is installed, NOT the Python bindings to libmagic API \n'
  81. 'see https://github.com/ahupp/python-magic\n')
  82. exit(1)
  83. VERSION = "prismedia v0.6.1-1"
  84. VALID_PRIVACY_STATUSES = ('public', 'private', 'unlisted')
  85. VALID_CATEGORIES = (
  86. "music", "films", "vehicles",
  87. "sports", "travels", "gaming", "people",
  88. "comedy", "entertainment", "news",
  89. "how to", "education", "activism", "science & technology",
  90. "science", "technology", "animals"
  91. )
  92. VALID_PLATFORM = ('youtube', 'peertube')
  93. VALID_LANGUAGES = ('arabic', 'english', 'french',
  94. 'german', 'hindi', 'italian',
  95. 'japanese', 'korean', 'mandarin',
  96. 'portuguese', 'punjabi', 'russian', 'spanish')
  97. def validateVideo(path):
  98. supported_types = ['video/mp4']
  99. if magic.from_file(path, mime=True) in supported_types:
  100. return path
  101. else:
  102. return False
  103. def validateCategory(category):
  104. if category.lower() in VALID_CATEGORIES:
  105. return True
  106. else:
  107. return False
  108. def validatePrivacy(privacy):
  109. if privacy.lower() in VALID_PRIVACY_STATUSES:
  110. return True
  111. else:
  112. return False
  113. def validatePlatform(platform):
  114. for plfrm in platform.split(','):
  115. if plfrm.lower().replace(" ", "") not in VALID_PLATFORM:
  116. return False
  117. return True
  118. def validateLanguage(language):
  119. if language.lower() in VALID_LANGUAGES:
  120. return True
  121. else:
  122. return False
  123. def validatePublish(publish):
  124. # Check date format and if date is future
  125. try:
  126. now = datetime.datetime.now()
  127. publishAt = datetime.datetime.strptime(publish, '%Y-%m-%dT%H:%M:%S')
  128. if now >= publishAt:
  129. return False
  130. except ValueError:
  131. return False
  132. return True
  133. def validateThumbnail(thumbnail):
  134. supported_types = ['image/jpg', 'image/jpeg']
  135. if magic.from_file(thumbnail, mime=True) in supported_types:
  136. return thumbnail
  137. else:
  138. return False
  139. if __name__ == '__main__':
  140. options = docopt(__doc__, version=VERSION)
  141. schema = Schema({
  142. '--file': And(str, validateVideo, error='file is not supported, please use mp4'),
  143. Optional('--name'): Or(None, And(
  144. str,
  145. lambda x: not x.isdigit(),
  146. error="The video name should be a string")
  147. ),
  148. Optional('--description'): Or(None, And(
  149. str,
  150. lambda x: not x.isdigit(),
  151. error="The video name should be a string")
  152. ),
  153. Optional('--tags'): Or(None, And(
  154. str,
  155. lambda x: not x.isdigit(),
  156. error="Tags should be a string")
  157. ),
  158. Optional('--category'): Or(None, And(
  159. str,
  160. validateCategory,
  161. error="Category not recognized, please see --help")
  162. ),
  163. Optional('--language'): Or(None, And(
  164. str,
  165. validateLanguage,
  166. error="Language not recognized, please see --help")
  167. ),
  168. Optional('--privacy'): Or(None, And(
  169. str,
  170. validatePrivacy,
  171. error="Please use recognized privacy between public, unlisted or private")
  172. ),
  173. Optional('--nfo'): Or(None, str),
  174. Optional('--platform'): Or(None, And(str, validatePlatform, error="Sorry, upload platform not supported")),
  175. Optional('--publishAt'): Or(None, And(
  176. str,
  177. validatePublish,
  178. error="DATE should be the form YYYY-MM-DDThh:mm:ss and has to be in the future")
  179. ),
  180. Optional('--cca'): bool,
  181. Optional('--disable-comments'): bool,
  182. Optional('--nsfw'): bool,
  183. Optional('--thumbnail'): Or(None, And(
  184. str, validateThumbnail, error='thumbnail is not supported, please use jpg/jpeg'),
  185. ),
  186. Optional('--playlist'): Or(None, str),
  187. Optional('--playlistCreate'): bool,
  188. '--help': bool,
  189. '--version': bool
  190. })
  191. options = utils.parseNFO(options)
  192. if not options.get('--thumbnail'):
  193. options = utils.searchThumbnail(options)
  194. try:
  195. options = schema.validate(options)
  196. except SchemaError as e:
  197. exit(e)
  198. if options.get('--platform') is None or "youtube" in options.get('--platform'):
  199. yt_upload.run(options)
  200. if options.get('--platform') is None or "peertube" in options.get('--platform'):
  201. pt_upload.run(options)