Scripting way to upload videos to peertube and youtube
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

prismedia_upload.py 9.7KB

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