LoginSignup
14
13

More than 5 years have passed since last update.

[Swift] Twitterに投稿出来る動画かどうかチェックする

Last updated at Posted at 2016-03-11

アップロード出来る動画の制限

簡単にまとめると

項目 説明
ファイルサイズ ~15MB
秒数 0.5 〜 30
大きさ 32x32 〜 1280x1024
アスペクト比 1:3 〜 3:1
フレーム 〜 40fps
オーディオ mono、stereo

https://syncer.jp/twitter-api-matome/post/media/upload_chunked より引用

ということらしいです。(ビデオの場合)

Validate!

ファイルサイズ

      //ファイル取り出し
      var fileAttr = [String : AnyObject]()
      do {
        let fileManager = NSFileManager.defaultManager()
        fileAttr = try fileManager.attributesOfItemAtPath(filePath)
      } catch {}
      //ビデオのファイルサイズチェック
      guard let fileSize = fileAttr[NSFileSize] as? Int else {return}
      if fileSize <= 15000000 {
        // OK!
      }

秒数

      let asset = AVAsset(URL: NSURL(fileURLWithPath: filePath))
      guard let videoAssetTrack = asset.tracksWithMediaType(AVMediaTypeVideo).first else { return }
      let duration = CMTimeGetSeconds(videoAssetTrack.timeRange.duration)
      if 0.5...30.0 ~= duration {
        //ok
      }

大きさ

      let videoWidth = videoAssetTrack.naturalSize.width
      let videoHeight = videoAssetTrack.naturalSize.height
      let smallLength = min(videoWidth, videoHeight)
      let longLength  = max(videoWidth, videoHeight)
      if longLength <= 1280 && 32...1024 ~= smallLength {
        //ok
      }

アス比

      let aspectRatio = longLength/smallLength
      if aspectRatio <= 3.0 {
        //ok
      }

フレームレート


      let fps = videoAssetTrack.nominalFrameRate
      if fps <= 40 {
        // ok
      }
14
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
13