<commit_before>//
//  ViewController.swift
//  LearningVideoComposition
//
//  Created by <NAME> on 2014/12/17.
//  Copyright (c) 2014年 <NAME>. All rights reserved.
//

import UIKit
import Photos
import AVFoundation

/// フレームレート
public let kFrameRate: Int = 60

class ViewController: UIViewController {

    @IBOutlet weak var playerView: HKLGLPixelBufferView!
    @IBOutlet weak var msgLabel: UILabel!
    private let _player = HKLAVGaplessPlayer()

    @IBOutlet weak var rateLabel: UILabel!
    private var _timer: NSTimer!

    override func viewDidLoad() {
        super.viewDidLoad()

        _player.playerView = playerView
        loadVideoAssets()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        _timer = NSTimer.scheduledTimerWithTimeInterval(
            0.2, target: self, selector: "updateCpuInfo:",
            userInfo: nil, repeats: true)
    }

    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)

        _timer.invalidate()
    }

    @IBAction func tapped(sender: AnyObject) {
        if _player.isPlaying {
            _player.pause()
        } else {
            _player.play()
        }
    }

    @IBAction func sliderUpdated(sender: UISlider) {
        let result = _player._producer.playerInfoForPosition(sender.value)
        println("position:\(sender.value) -> \(result)")
    }

    @IBAction func rateChanged(sender: UISlider) {
        rateLabel.text = "rate: \(sender.value)"
        _player.play(rate: sender.value)
    }

    @objc func updateCpuInfo(timer: NSTimer) {
        msgLabel.text = "cpu: \(cpu_usage_in_percent())%"
    }

    /**
    カメラロールから古い順で10個のビデオを取り出し、リーダーをセットアップする
    */
    private func loadVideoAssets() {

        let queue = dispatch_queue_create("buildingqueue", DISPATCH_QUEUE_SERIAL)

        let collections = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype:.SmartAlbumVideos, options: nil)
        collections.enumerateObjectsUsingBlock {
            [unowned self]  collection, index, stop  in

            // 日付の古い順
            var options = PHFetchOptions()
            options.sortDescriptors = [ NSSortDescriptor(key: "creationDate", ascending: true) ]

            let assets = PHAsset.fetchAssetsInAssetCollection(collection as PHAssetCollection, options: options)
            assets.enumerateObjectsUsingBlock { asset, index, stop in

                // この処理は非同期で行われる
                _ = PHImageManager.defaultManager().requestAVAssetForVideo(asset as PHAsset, options:nil) {
                    avasset, audioMix, info in

                    if let avasset = avasset {
                        dispatch_async(queue) {
                            // プロデューサーにアセットを追加
                            self._player.appendAsset(avasset)

                            let track = avasset.tracksWithMediaType(AVMediaTypeVideo)[0] as AVAssetTrack
                        }
                    }
                }
            }
        }
    }

}

<commit_msg>Collect videos ordered in creationDate.<commit_after>//
//  ViewController.swift
//  LearningVideoComposition
//
//  Created by <NAME> on 2014/12/17.
//  Copyright (c) 2014年 <NAME>. All rights reserved.
//

import UIKit
import Photos
import AVFoundation

/// フレームレート
public let kFrameRate: Int = 60

class ViewController: UIViewController {

    @IBOutlet weak var playerView: HKLGLPixelBufferView!
    @IBOutlet weak var msgLabel: UILabel!
    private let _player = HKLAVGaplessPlayer()

    @IBOutlet weak var rateLabel: UILabel!
    private var _timer: NSTimer!

    override func viewDidLoad() {
        super.viewDidLoad()

        _player.playerView = playerView
        loadVideoAssets()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        _timer = NSTimer.scheduledTimerWithTimeInterval(
            0.2, target: self, selector: "updateCpuInfo:",
            userInfo: nil, repeats: true)
    }

    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)

        _timer.invalidate()
    }

    @IBAction func tapped(sender: AnyObject) {
        if _player.isPlaying {
            _player.pause()
        } else {
            _player.play()
        }
    }

    @IBAction func sliderUpdated(sender: UISlider) {
        let result = _player._producer.playerInfoForPosition(sender.value)
        println("position:\(sender.value) -> \(result)")
    }

    @IBAction func rateChanged(sender: UISlider) {
        rateLabel.text = "rate: \(sender.value)"
        _player.play(rate: sender.value)
    }

    @objc func updateCpuInfo(timer: NSTimer) {
        msgLabel.text = "cpu: \(cpu_usage_in_percent())%"
    }

    /**
    カメラロールから古い順で10個のビデオを取り出し、リーダーをセットアップする
    */
    private func loadVideoAssets() {

        let queue = dispatch_queue_create("buildingqueue", DISPATCH_QUEUE_SERIAL)

        // 収集したアセットをいったん格納する（最終的にindex順でソートして格納）
        var avassets = [(Int, AVAsset)]()

        // 「ビデオ」のスマートアルバムから収集
        let collections = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype:.SmartAlbumVideos, options: nil)
        collections.enumerateObjectsUsingBlock {
            [unowned self]  collection, index, stop  in
            let collection = collection as PHAssetCollection

            // 日付の古い順に取得
            var options = PHFetchOptions()
            options.sortDescriptors = [ NSSortDescriptor(key: "creationDate", ascending: true) ]

            let assets = PHAsset.fetchAssetsInAssetCollection(collection, options: options)
            assets.enumerateObjectsUsingBlock {
                phAsset, index, stop in
                let phAsset = phAsset as PHAsset

                // この処理は非同期で行われるので注意
                _ = PHImageManager.defaultManager().requestAVAssetForVideo(phAsset, options:nil) {
                    avasset, audioMix, info in

                    if let avasset = avasset {
                        avassets.append((index,avasset))
                        if avassets.count == assets.count {
                            dispatch_async(queue) {
                                // プロデューサーにアセットを追加
                                sort(&avassets) { $0.0 < $1.0 }
                                for (_, a) in avassets {
                                    self._player.appendAsset(a)
                                }
                            }
                        }

                    }
                }
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.08794740686632578, 'fraction_numerical': 0.005989773557341125, 'mean_word_length': 2.003949100482668, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2220479', 'n_tokens_mistral': 2136, 'n_tokens_neox': 1989, 'n_words': 404}