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.

269 lines
11 KiB

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 python
  2. # coding: utf-8
  3. """
  4. prismedia - tool to upload videos to Peertube and Youtube
  5. Usage:
  6. prismedia --file=<FILE> [options]
  7. prismedia -f <FILE> --tags=STRING [options]
  8. prismedia -h | --help
  9. prismedia --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. --debug Trigger some debug information like options used (default: no)
  14. -d, --description=STRING Description of the video. (default: default description)
  15. -t, --tags=STRING Tags for the video. comma separated.
  16. WARN: tags with punctuation (!, ', ", ?, ...)
  17. are not supported by Mastodon to be published from Peertube
  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 the video name and will
  25. decode the file as UTF-8 (so make sure your nfo file is UTF-8 encoded)
  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. --publishAt=DATE Publish the video at the given DATE using local server timezone.
  31. DATE should be on the form YYYY-MM-DDThh:mm:ss eg: 2018-03-12T19:00:00
  32. DATE should be in the future
  33. --peertubeAt=DATE
  34. --youtubeAt=DATE Override publishAt for the corresponding platform. Allow to create preview on specific platform
  35. --thumbnail=STRING Path to a file to use as a thumbnail for the video.
  36. Supported types are jpg and jpeg.
  37. By default, prismedia search for an image based on video name followed by .jpg or .jpeg
  38. --channel=STRING Set the channel to use for the video (Peertube only)
  39. If the channel is not found, spawn an error except if --channelCreate is set.
  40. --channelCreate Create the channel if not exists. (Peertube only, default do not create)
  41. Only relevant if --channel is set.
  42. --playlist=STRING Set the playlist to use for the video.
  43. If the playlist is not found, spawn an error except if --playlistCreate is set.
  44. --playlistCreate Create the playlist if not exists. (default do not create)
  45. Only relevant if --playlist is set.
  46. -h --help Show this help.
  47. --version Show version.
  48. Categories:
  49. Category is the type of video you upload. Default is films.
  50. Here are available categories from Peertube and Youtube:
  51. music, films, vehicles,
  52. sports, travels, gaming, people,
  53. comedy, entertainment, news,
  54. how to, education, activism, science & technology,
  55. science, technology, animals
  56. Languages:
  57. Language of the video (audio track), choose one. Default is English
  58. Here are available languages from Peertube and Youtube:
  59. Arabic, English, French, German, Hindi, Italian,
  60. Japanese, Korean, Mandarin, Portuguese, Punjabi, Russian, Spanish
  61. """
  62. import sys
  63. if sys.version_info[0] < 3:
  64. raise Exception("Python 3 or a more recent version is required.")
  65. import datetime
  66. import logging
  67. logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
  68. from docopt import docopt
  69. from . import yt_upload
  70. from . import pt_upload
  71. from . import utils
  72. try:
  73. # noinspection PyUnresolvedReferences
  74. from schema import Schema, And, Or, Optional, SchemaError
  75. except ImportError:
  76. logging.error('This program requires that the `schema` data-validation library'
  77. ' is installed: \n'
  78. 'see https://github.com/halst/schema\n')
  79. exit(1)
  80. try:
  81. # noinspection PyUnresolvedReferences
  82. import magic
  83. except ImportError:
  84. logging.error('This program requires that the `python-magic` library'
  85. ' is installed, NOT the Python bindings to libmagic API \n'
  86. 'see https://github.com/ahupp/python-magic\n')
  87. exit(1)
  88. VERSION = "prismedia v0.9.0"
  89. VALID_PRIVACY_STATUSES = ('public', 'private', 'unlisted')
  90. VALID_CATEGORIES = (
  91. "music", "films", "vehicles",
  92. "sports", "travels", "gaming", "people",
  93. "comedy", "entertainment", "news",
  94. "how to", "education", "activism", "science & technology",
  95. "science", "technology", "animals"
  96. )
  97. VALID_PLATFORM = ('youtube', 'peertube', 'none')
  98. VALID_LANGUAGES = ('arabic', 'english', 'french',
  99. 'german', 'hindi', 'italian',
  100. 'japanese', 'korean', 'mandarin',
  101. 'portuguese', 'punjabi', 'russian', 'spanish')
  102. def validateVideo(path):
  103. supported_types = ['video/mp4']
  104. detected_type = magic.from_file(path, mime=True)
  105. if detected_type not in supported_types:
  106. print("File", path, "detected type is", detected_type, "which is not one of", supported_types)
  107. force_file = ['y', 'yes']
  108. is_forcing = input("Are you sure you selected the correct file? (y/N)")
  109. if is_forcing.lower() not in force_file:
  110. return False
  111. return path
  112. def validateCategory(category):
  113. if category.lower() in VALID_CATEGORIES:
  114. return True
  115. else:
  116. return False
  117. def validatePrivacy(privacy):
  118. if privacy.lower() in VALID_PRIVACY_STATUSES:
  119. return True
  120. else:
  121. return False
  122. def validatePlatform(platform):
  123. for plfrm in platform.split(','):
  124. if plfrm.lower().replace(" ", "") not in VALID_PLATFORM:
  125. return False
  126. return True
  127. def validateLanguage(language):
  128. if language.lower() in VALID_LANGUAGES:
  129. return True
  130. else:
  131. return False
  132. def validatePublish(publish):
  133. # Check date format and if date is future
  134. try:
  135. now = datetime.datetime.now()
  136. publishAt = datetime.datetime.strptime(publish, '%Y-%m-%dT%H:%M:%S')
  137. if now >= publishAt:
  138. return False
  139. except ValueError:
  140. return False
  141. return True
  142. def validateThumbnail(thumbnail):
  143. supported_types = ['image/jpg', 'image/jpeg']
  144. if magic.from_file(thumbnail, mime=True) in supported_types:
  145. return thumbnail
  146. else:
  147. return False
  148. def main():
  149. options = docopt(__doc__, version=VERSION)
  150. schema = Schema({
  151. '--file': And(str, validateVideo, error='file is not supported, please use mp4'),
  152. Optional('--name'): Or(None, And(
  153. str,
  154. lambda x: not x.isdigit(),
  155. error="The video name should be a string")
  156. ),
  157. Optional('--description'): Or(None, And(
  158. str,
  159. lambda x: not x.isdigit(),
  160. error="The video description should be a string")
  161. ),
  162. Optional('--tags'): Or(None, And(
  163. str,
  164. lambda x: not x.isdigit(),
  165. error="Tags should be a string")
  166. ),
  167. Optional('--category'): Or(None, And(
  168. str,
  169. validateCategory,
  170. error="Category not recognized, please see --help")
  171. ),
  172. Optional('--language'): Or(None, And(
  173. str,
  174. validateLanguage,
  175. error="Language not recognized, please see --help")
  176. ),
  177. Optional('--privacy'): Or(None, And(
  178. str,
  179. validatePrivacy,
  180. error="Please use recognized privacy between public, unlisted or private")
  181. ),
  182. Optional('--nfo'): Or(None, str),
  183. Optional('--platform'): Or(None, And(str, validatePlatform, error="Sorry, upload platform not supported")),
  184. Optional('--publishAt'): Or(None, And(
  185. str,
  186. validatePublish,
  187. error="DATE should be the form YYYY-MM-DDThh:mm:ss and has to be in the future")
  188. ),
  189. Optional('--peertubeAt'): Or(None, And(
  190. str,
  191. validatePublish,
  192. error="DATE should be the form YYYY-MM-DDThh:mm:ss and has to be in the future")
  193. ),
  194. Optional('--youtubeAt'): Or(None, And(
  195. str,
  196. validatePublish,
  197. error="DATE should be the form YYYY-MM-DDThh:mm:ss and has to be in the future")
  198. ),
  199. Optional('--debug'): bool,
  200. Optional('--cca'): bool,
  201. Optional('--disable-comments'): bool,
  202. Optional('--nsfw'): bool,
  203. Optional('--thumbnail'): Or(None, And(
  204. str, validateThumbnail, error='thumbnail is not supported, please use jpg/jpeg'),
  205. ),
  206. Optional('--channel'): Or(None, str),
  207. Optional('--channelCreate'): bool,
  208. Optional('--playlist'): Or(None, str),
  209. Optional('--playlistCreate'): bool,
  210. '--help': bool,
  211. '--version': bool
  212. })
  213. options = utils.parseNFO(options)
  214. if not options.get('--thumbnail'):
  215. options = utils.searchThumbnail(options)
  216. try:
  217. options = schema.validate(options)
  218. except SchemaError as e:
  219. exit(e)
  220. if options.get('--debug'):
  221. print(sys.version)
  222. print(options)
  223. if options.get('--platform') is None or "peertube" in options.get('--platform'):
  224. pt_upload.run(options)
  225. if options.get('--platform') is None or "youtube" in options.get('--platform'):
  226. yt_upload.run(options)
  227. if __name__ == '__main__':
  228. import warnings
  229. warnings.warn("use 'python -m prismedia', not 'python -m prismedia.upload'", DeprecationWarning)
  230. main()