omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    How videos add to the camera roll

    Pythonista
    photos image video camera roll
    4
    18
    7806
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • beans
      beans @cvp last edited by

      @cvp I tried the code. I can add the video to specified album. However, the code creates the album every time when adding another video to the album with same name (many 'my_album' are created); can not add the video to existing album. Any information or alternative way ?

      cvp 2 Replies Last reply Reply Quote 0
      • cvp
        cvp @beans last edited by

        @beans sorry for that, I have only tested once. I'll try in the afternoon Brussels time

        1 Reply Last reply Reply Quote 0
        • cvp
          cvp @beans last edited by cvp

          @beans that should be ok, I hope

          # add a local video file to IOS photos album
          from objc_util import *
          import threading
          
          PHPhotoLibrary = ObjCClass('PHPhotoLibrary')
          PHAssetChangeRequest = ObjCClass('PHAssetChangeRequest')
          PHAssetCollectionChangeRequest = ObjCClass('PHAssetCollectionChangeRequest')
          PHAssetCollection = ObjCClass('PHAssetCollection')
          PHFetchOptions = ObjCClass('PHFetchOptions').alloc()
          
          def add_video():
              lib = PHPhotoLibrary.sharedPhotoLibrary()
              url = nsurl('MesTests/mov_bbb.mp4')         # path to local video file
              
              PHAssetCollectionType = 1			# album
              PHAssetCollectionSubtype = 2 	# albumregular
              album = 'my_album'
              NSPredicate = ObjCClass('NSPredicate').predicateWithFormat_argumentArray_("title = %@", [album])
              PHFetchOptions.setPredicate_(NSPredicate)
              # get number of albums with this name
              fetchresult = PHAssetCollection.fetchAssetCollectionsWithType_subtype_options_(PHAssetCollectionType, PHAssetCollectionSubtype, PHFetchOptions)
              n_albums = fetchresult.count()
          
              def change_block():
                  req = PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL_(url)
                  placeholderForCreatedAsset = req.placeholderForCreatedAsset()
                  
                  if n_albums == 0:
                      print('create album',album)
                      albumChangeRequest = PHAssetCollectionChangeRequest.creationRequestForAssetCollectionWithTitle_(album)
                  else:
                      print('existing album',album)
                      albumChangeRequest = PHAssetCollectionChangeRequest.changeRequestForAssetCollection_(fetchresult.firstObject())
                  print('add video')
                  albumChangeRequest.addAssets_([placeholderForCreatedAsset])
          
          
              def perform_changes():
                  lib.performChangesAndWait_error_(change_block, None)
                  
              t = threading.Thread(target=perform_changes)
              t.start()
              t.join()
          
          if __name__ == '__main__':
              add_video() 
          
          beans 1 Reply Last reply Reply Quote 2
          • beans
            beans @cvp last edited by

            @cvp Thank you very much. The code works as expected. Very helpful. Additionally I found another issue through testing the code: the file should be accessible as internal file, the file is only stored in iCloud Drive (not imported to the device as accessible) can not be added. I assume this is reasonable but if any way to start importing from iCloud Drive to the device by the code it will be further helpful. Any ideas ? Sorry for repeated asking.

            cvp 1 Reply Last reply Reply Quote 0
            • cvp
              cvp @beans last edited by

              @beans said:

              the file is only stored in iCloud Drive

              In which folder? Try to open it as "external folder" in Pythonista files browser

              cvp 1 Reply Last reply Reply Quote 0
              • cvp
                cvp @cvp last edited by cvp

                @beans Once you have defined your iCloud Drive folder as external folder in Pythonista, you can get a file with

                    url = nsurl('/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs/MyFolder/mov_bbb.mp4')
                
                beans 1 Reply Last reply Reply Quote 0
                • beans
                  beans @cvp last edited by

                  @cvp I opened that iCloud folder from EXTERNAL FILES. The folder is shown in EXTERNAL FILES. The files existed in the folder are accessible as '/private/var...', however, the files newly added to that folder are not shown in the folder of EXTERNAL FILES and are not accessible as '/private/var...' The newly added files can be shown using os.listdir('/private/var...'). The file names are shown as '.filename.icloud' (they are not shown in the folder of EXTERNAL FILES). I assume the files are not imported to the device or not synced with EXTERNAL FILES and would like to know if any way to import the files by the code.

                  cvp 1 Reply Last reply Reply Quote 0
                  • cvp
                    cvp @beans last edited by cvp

                    @beans it is a known problem: a file not yet locally downloaded is not accessible 😢
                    If you first download it in the Files app, you should be able to access it.
                    I think there are some topics in this forum about this problem.
                    Sorry for you

                    beans 1 Reply Last reply Reply Quote 0
                    • beans
                      beans @cvp last edited by

                      @cvp Thank you for the information. Anyway, the code is very useful.

                      1 Reply Last reply Reply Quote 0
                      • henryaukc
                        henryaukc last edited by

                        yeah. it works

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post
                        Powered by NodeBB Forums | Contributors