/converting.py

https://github.com/MechanisM/YTupload · Python · 69 lines · 44 code · 7 blank · 18 comment · 3 complexity · 78ac912831b1def718cb943394b2536c MD5 · raw file

  1. # -*- coding: utf-8 -*-
  2. import settings, os, datetime
  3. os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
  4. from control.models import *
  5. # если уже, что-то конвертится - выходим
  6. # if something already converting - quit
  7. if Log.objects.filter(status=Log.CONVERTING):
  8. quit()
  9. else:
  10. # находим первый файл в очереди
  11. # find first file in queue
  12. log_list = Log.objects.filter(status=Log.QUEUED)
  13. if not log_list:
  14. quit()
  15. # меняем статус на "конвертируется"
  16. # change status to "converting"
  17. file = log_list[0]
  18. file.status = Log.CONVERTING
  19. file.save()
  20. # переназываем файл
  21. # rename file
  22. basename, extension = os.path.splitext(file.filename)
  23. newfilename = basename + '.flv'
  24. # запускаем конвертирование
  25. # start converting
  26. os.system('nice -n 20 ffmpeg -i ' + os.path.join(settings.ENCODE_DIR_FROM, file.filename) + \
  27. ' -ar ' + settings.FFMPEG_AR + \
  28. ' -vb ' + settings.FFMPEG_VB + \
  29. ' ' + os.path.join(settings.ENCODE_DIR_TO, newfilename))
  30. file.status = Log.UPLOADING
  31. file.save()
  32. # загружаем на youtube
  33. # start uploading to youtube
  34. import gdata.youtube
  35. import gdata.youtube.service
  36. yt_service = gdata.youtube.service.YouTubeService()
  37. # A complete client login request
  38. yt_service.email = settings.YT_LOGIN
  39. yt_service.password = settings.YT_PASSWORD
  40. yt_service.developer_key = settings.YT_DEVKEY
  41. yt_service.ProgrammaticLogin()
  42. # prepare a media group object to hold our video's meta-data
  43. my_media_group = gdata.media.Group(
  44. title=gdata.media.Title(text=file.title.encode('utf-8')),
  45. description=gdata.media.Description(description_type='plain', text=file.description.encode('utf-8')),
  46. keywords=gdata.media.Keywords(text=file.keywords.encode('utf-8')),
  47. category=[gdata.media.Category(
  48. text='News',
  49. scheme='http://gdata.youtube.com/schemas/2007/categories.cat',
  50. label='News')],
  51. player=None
  52. )
  53. # create the gdata.youtube.YouTubeVideoEntry to be uploaded
  54. video_entry = gdata.youtube.YouTubeVideoEntry(media=my_media_group)
  55. # set the path for the video file binary
  56. video_file_location = os.path.join(settings.ENCODE_DIR_TO, newfilename).encode("utf-8")
  57. new_entry = yt_service.InsertVideoEntry(video_entry, video_file_location)
  58. # completed upload, check link
  59. file.link = new_entry.media.player.url
  60. file.status = Log.UPLOADED
  61. file.uploaded = datetime.datetime.now()
  62. file.save()