Repository navigation
Add a "Screenshot Output" Hotkey That Saves the Program Output as a PNG #6231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
au-sf
wants to merge
6
commits into
staging
Choose a base branch
from
1216010475168255-screenshot-output-hotkey
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b37a34a
feat: add a Screenshot Output hotkey that saves the program output as…
au-sf 45f753e
Await the non-blocking OBS_content_takeScreenshot
summeroff ac8b46f
Use only the requested display's video context for screenshots
summeroff e9a4a17
Merge remote-tracking branch 'origin/staging' into 1216010475168255-s…
au-sf eac726c
test: Screenshot Output writes a PNG to the recording folder and repo…
au-sf 4d7d435
test: assert the Screenshot Output success toast in the footer [Asana…
au-sf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import fs from 'fs'; | ||
| import path from 'path'; | ||
| import * as remote from '@electron/remote'; | ||
| import { Inject, Service } from 'services/core'; | ||
| import * as obs from '../../obs-api'; | ||
| import { $t } from './i18n'; | ||
| import { SettingsService } from './settings'; | ||
| import { OutputSettingsService } from './settings/output'; | ||
| import { TDisplayType, VideoSettingsService } from './settings-v2/video'; | ||
| import { ENotificationType, NotificationsService } from './notifications'; | ||
| import { JsonrpcService } from './api/jsonrpc'; | ||
| import { UsageStatisticsService } from './usage-statistics'; | ||
|
|
||
| /** OBS's default "Filename Formatting", used when the setting is empty. */ | ||
| const DEFAULT_FILENAME_FORMAT = '%CCYY-%MM-%DD %hh-%mm-%ss'; | ||
|
|
||
| /** | ||
| * Saves a still of the program output, the way OBS Studio's "Screenshot Output" | ||
| * hotkey does: a PNG at canvas resolution, written to the recording folder as | ||
| * `Screenshot <Filename Formatting>.png`. The capture itself happens in the OBS | ||
| * backend (`OBS_content_takeScreenshot`); this service supplies the settings and | ||
| * reports the result. | ||
| */ | ||
| export class ScreenshotService extends Service { | ||
| @Inject() private settingsService: SettingsService; | ||
| @Inject() private outputSettingsService: OutputSettingsService; | ||
| @Inject() private videoSettingsService: VideoSettingsService; | ||
| @Inject() private notificationsService: NotificationsService; | ||
| @Inject() private jsonrpcService: JsonrpcService; | ||
| @Inject() private usageStatisticsService: UsageStatisticsService; | ||
|
|
||
| async takeScreenshot(display: TDisplayType = 'horizontal') { | ||
| if (typeof obs.NodeObs.OBS_content_takeScreenshot !== 'function') { | ||
| this.warn($t('Screenshots need a newer version of the Streamlabs backend.')); | ||
| return; | ||
| } | ||
|
|
||
| const context = this.videoSettingsService.contexts[display]; | ||
| if (!context) { | ||
| this.warn($t('Could not save screenshot')); | ||
| return; | ||
| } | ||
|
|
||
| const directory = this.outputSettingsService.getRecordingSettings(display).path; | ||
| if (!directory || !fs.existsSync(directory)) { | ||
| this.warn($t('Set a recording folder in Settings > Output before taking a screenshot.')); | ||
| return; | ||
| } | ||
|
|
||
| const output = this.settingsService.state.Output.formData; | ||
| const advanced = this.settingsService.state.Advanced.formData; | ||
| const mode = this.settingsService.findSettingValue(output, 'Untitled', 'Mode'); | ||
| const noSpaceKey = mode === 'Advanced' ? 'RecFileNameWithoutSpace' : 'FileNameWithoutSpace'; | ||
| const noSpace = !!this.settingsService.findSettingValue(output, 'Recording', noSpaceKey); | ||
| const format: string = | ||
| this.settingsService.findSettingValue(advanced, 'Recording', 'FilenameFormatting') || | ||
| DEFAULT_FILENAME_FORMAT; | ||
|
|
||
| try { | ||
| const result = await obs.NodeObs.OBS_content_takeScreenshot( | ||
| context, | ||
| directory, | ||
| format, | ||
| noSpace, | ||
| ); | ||
| this.notificationsService.push({ | ||
| type: ENotificationType.SUCCESS, | ||
| message: $t('Screenshot saved as %{filename}', { filename: path.basename(result.path) }), | ||
| action: this.jsonrpcService.createRequest( | ||
| Service.getResourceId(this), | ||
| 'showScreenshot', | ||
| result.path, | ||
| ), | ||
| }); | ||
| this.usageStatisticsService.recordFeatureUsage('ScreenshotOutput'); | ||
| } catch (e: unknown) { | ||
| console.error('Failed to take screenshot', e); | ||
| this.warn($t('Could not save screenshot')); | ||
| } | ||
| } | ||
|
|
||
| showScreenshot(filePath: string) { | ||
| remote.shell.showItemInFolder(filePath); | ||
| } | ||
|
|
||
| private warn(message: string) { | ||
| this.notificationsService.push({ type: ENotificationType.WARNING, message }); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { readdir, readFile } from 'fs-extra'; | ||
| import * as path from 'path'; | ||
| import { test, useWebdriver } from '../helpers/webdriver'; | ||
| import { getApiClient } from '../helpers/api-client'; | ||
| import { setTemporaryRecordingPath } from '../helpers/modules/settings/settings'; | ||
| import { focusMain, waitForDisplayed } from '../helpers/modules/core'; | ||
| import { ScreenshotService } from 'app-services'; | ||
|
|
||
| // not a react hook | ||
| // eslint-disable-next-line react-hooks/rules-of-hooks | ||
| useWebdriver(); | ||
|
|
||
| const PNG_SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]); | ||
|
|
||
| test('Screenshot Output saves a PNG of the program output to the recording folder', async t => { | ||
| const recordingDir = await setTemporaryRecordingPath(); | ||
| await focusMain(); | ||
|
|
||
| const client = await getApiClient(); | ||
| const screenshotService = client.getResource<ScreenshotService>('ScreenshotService'); | ||
|
|
||
| await screenshotService.takeScreenshot(); | ||
|
summeroff marked this conversation as resolved.
|
||
|
|
||
| const files = (await readdir(recordingDir)).filter(file => /^Screenshot .+\.png$/.test(file)); | ||
| t.is(files.length, 1, 'exactly one screenshot was written to the recording folder'); | ||
|
|
||
| // The success toast in the footer is the only place the message is readable: the service | ||
| // filters notifications out of its API state, and the notifications window lists only | ||
| // performance notifications. | ||
| // Matched without the extension: webdriverio reads a selector ending in an image extension | ||
| // as a file path. | ||
| const savedAs = files[0].replace(/\.png$/, ''); | ||
| await waitForDisplayed(`div*=Screenshot saved as ${savedAs}`, { timeout: 5000 }); | ||
|
|
||
| const bytes = await readFile(path.join(recordingDir, files[0])); | ||
| t.true(bytes.length > PNG_SIGNATURE.length, 'the screenshot has a body'); | ||
| t.deepEqual(bytes.subarray(0, PNG_SIGNATURE.length), PNG_SIGNATURE, 'the screenshot is a PNG'); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed; deferred until the osn release with OBS_content_takeScreenshot is pinned. Before that, a test can only reach the needs-newer-backend path.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#6250