일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- Xcode
- Nodejs
- OSX
- fido
- css
- SSH
- albumbook
- git
- SwiftUI
- openssl
- 애플
- appres
- OTP
- 인증
- MYSQL
- 앱스토어
- MSYS2
- 앱리소스
- 2FA
- otpkey
- FIDO2
- 안드로이드
- MFA
- kmip
- WebAuthn
- SWIFT
- apple
- SSL
- 앨범북
- Android
Archives
- Today
- Total
인디노트
Swift 에서 화면(Screen) 캡춰와 녹화관련 본문
github.com/nirix/swift-screencapture
//
// AppDelegate.swift
// Example
//
// Created by Jack P. on 11/12/2015.
// Copyright © 2015 Jack P. All rights reserved.
//
import Cocoa
import AVKit
import AVFoundation
import ScreenCapture
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
@IBOutlet weak var imgView: NSImageView!
@IBOutlet weak var playerView: AVPlayerView!
@IBOutlet weak var startRecordingBtn: NSButton!
@IBOutlet weak var stopRecordingBtn: NSButton!
let tmpDir: String = NSTemporaryDirectory()
var player: AVPlayer?
var screenRecorder: ScreenCapture.ScreenRecorder?
func applicationDidFinishLaunching(aNotification: NSNotification) {
stopRecordingBtn.isEnabled = false
}
func applicationWillTerminate(aNotification: NSNotification) {
do {
if (FileManager.default.fileExists(atPath: "\(self.tmpDir)captureRegion.png")) {
try FileManager.default.removeItem(atPath: "\(self.tmpDir)captureRegion.png")
}
if (FileManager.default.fileExists(atPath: "\(self.tmpDir)captureScreen.png")) {
try FileManager.default.removeItem(atPath: "\(self.tmpDir)captureScreen.png")
}
if (FileManager.default.fileExists(atPath: "\(self.tmpDir)screenRecording.mp4")) {
try FileManager.default.removeItem(atPath: "\(self.tmpDir)screenRecording.mp4")
}
} catch {}
}
@IBAction func captureRegion(sender: NSButton) {
let imgPath: String = ScreenCapture.captureRegion("\(self.tmpDir)captureRegion.png").path
if (FileManager.default.fileExists(atPath: imgPath)) {
let img: NSImage = NSImage(contentsOfFile: imgPath)!
imgView.image = img
}
}
@IBAction func captureScreen(sender: NSButton) {
window.performMiniaturize(sender)
let imgPath: String = ScreenCapture.captureScreen("\(self.tmpDir)captureScreen.png").path
let img: NSImage = NSImage(contentsOfFile: imgPath)!
imgView.image = img
}
@IBAction func startRecording(sender: NSButton) {
do {
if (FileManager.default.fileExists(atPath: "\(self.tmpDir)screenRecording.mp4")) {
try FileManager.default.removeItem(atPath: "\(self.tmpDir)screenRecording.mp4")
}
} catch {}
startRecordingBtn.isEnabled = false
stopRecordingBtn.isEnabled = true
screenRecorder = ScreenCapture.recordScreen("\(self.tmpDir)screenRecording.mp4")
screenRecorder!.start()
}
@IBAction func stopRecording(sender: NSButton) {
screenRecorder!.stop()
startRecordingBtn.isEnabled = true
stopRecordingBtn.isEnabled = false
debugPrint(screenRecorder!.destination)
}
// It's better to seperate the playback function cause the stopRunning / writing video process may take some time
@IBAction func playback(_ sender: NSButton) {
self.player = AVPlayer(url: screenRecorder!.destination)
self.playerView.player = self.player
self.playerView.player?.play()
}
}
//
// ScreenCapture.swift
// ScreenCapture
//
// Created by Jack P. on 11/12/2015.
// Copyright © 2015 Jack P. All rights reserved.
//
import Foundation
// -------------------------------------------------------------------
// Allow passing of strings, just convert them to an NSURL.
public func captureRegion(_ destination: String) -> URL {
return captureRegion(URL(fileURLWithPath: destination))
}
public func captureScreen(_ destination: String) -> URL {
return captureScreen(URL(fileURLWithPath: destination))
}
public func recordScreen(_ destination: String) -> ScreenRecorder {
return recordScreen(URL(fileURLWithPath: destination))
}
// -------------------------------------------------------------------
public func captureRegion(_ destination: URL) -> URL {
let destinationPath = destination.path as String
let task = Process()
task.launchPath = "/usr/sbin/screencapture"
task.arguments = ["-i", "-r", destinationPath]
task.launch()
task.waitUntilExit()
return destination
}
public func captureScreen(_ destination: URL) -> URL {
let img = CGDisplayCreateImage(CGMainDisplayID())
let dest = CGImageDestinationCreateWithURL(destination as CFURL, kUTTypePNG, 1, nil)
CGImageDestinationAddImage(dest!, img!, nil)
CGImageDestinationFinalize(dest!)
return destination
}
public func recordScreen(_ destination: URL) -> ScreenRecorder {
return ScreenRecorder(destination: destination)
}
//
// ScreenRecorder.swift
// ScreenCapture
//
// Created by Jack P. on 11/12/2015.
// Copyright © 2015 Jack P. All rights reserved.
//
import Foundation
import AVFoundation
open class ScreenRecorder: NSObject, AVCaptureFileOutputRecordingDelegate {
public func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
}
let destinationUrl: URL
let session: AVCaptureSession
let movieFileOutput: AVCaptureMovieFileOutput
open var destination: URL {
get {
return self.destinationUrl
}
}
public init(destination: URL) {
destinationUrl = destination
session = AVCaptureSession()
session.sessionPreset = AVCaptureSession.Preset.high
let displayId: CGDirectDisplayID = CGDirectDisplayID(CGMainDisplayID())
let input: AVCaptureScreenInput = AVCaptureScreenInput(displayID: displayId)
if session.canAddInput(input) {
session.addInput(input)
}
movieFileOutput = AVCaptureMovieFileOutput()
if session.canAddOutput(movieFileOutput) {
session.addOutput(movieFileOutput)
}
}
open func start() {
session.startRunning()
movieFileOutput.startRecording(to: self.destinationUrl, recordingDelegate: self)
}
open func stop() {
movieFileOutput.stopRecording()
}
open func capture(
_ captureOutput: AVCaptureFileOutput!,
didFinishRecordingToOutputFileAt outputFileURL: URL!,
fromConnections connections: [Any]!,
error: Error!
) {
//
session.stopRunning()
}
}
반응형
'소스 팁 > Objective C, Swift, iOS, macOS' 카테고리의 다른 글
Swift substring !!! 왜 이케 만들었는지는 모르겠지만 좀 불편하다. (0) | 2021.05.13 |
---|---|
애플 인앱결재 관련 샘플코드 - Apple Developer Document (0) | 2021.04.21 |
Swift 의 class 안에서 다른 ViewController 를 여는 방법 (0) | 2021.04.17 |
macOS 앱 개발시 iOS 같은 UIImage 를 사용하고 싶은때 (0) | 2021.04.10 |
맥용 (macOS) 메뉴바 앱 (menu bar app) 만들기 - SwiftUI 편 (0) | 2021.04.09 |
Comments