Created
February 7, 2025 16:06
-
-
Save AldeRoberge/2e314909a5cd45cd05593c73925a08e4 to your computer and use it in GitHub Desktop.
Set the OBS output file name
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
using FluentResults; | |
using Microsoft.Extensions.Logging; | |
using Microsoft.Extensions.Options; | |
using OBSStudioClient; | |
using OBSStudioClient.Enums; | |
using Polly; | |
namespace ADG.Recording.OBS | |
{ | |
public class ObsServiceOptions | |
{ | |
public string Ip { get; set; } = "localhost"; | |
public int Port { get; set; } = 4455; | |
public string Password { get; set; } = "sfrUy5ZHxUIXAOFx"; | |
} | |
public interface IObsService | |
{ | |
Task<Result> UpdateOutputFileNameFormat(string fileNameFormatting); | |
} | |
public class ObsService(IOptions<ObsServiceOptions> options, ILogger<ObsService> logger) : IObsService | |
{ | |
private readonly ObsServiceOptions _options = options.Value; | |
public async Task<Result> UpdateOutputFileNameFormat(string fileNameFormatting) | |
{ | |
if (string.IsNullOrWhiteSpace(fileNameFormatting)) | |
return Result.Fail("File name formatting must not be empty."); | |
using var client = new ObsClient(); | |
logger.LogInformation("Connecting to OBS at {Ip}:{Port}", _options.Ip, _options.Port); | |
bool connectionResult = | |
await client.ConnectAsync( | |
password: _options.Password, | |
hostname: _options.Ip, | |
port: _options.Port | |
).ConfigureAwait(false); | |
if (!connectionResult) | |
{ | |
string errorMsg = $"Connection failed to OBS at {_options.Ip}:{_options.Port}. \n" + | |
"Is the WebSocket started? \n" + | |
"Tools > WebSocket Server > Enable"; | |
logger.LogError(errorMsg); | |
return Result.Fail(errorMsg); | |
} | |
logger.LogInformation("Connected to OBS. Waiting for Authentication to complete."); | |
// Await for authentication (client.ConnectionState == ConnectionState.Authenticated), max timeout 10 seconds | |
await Policy.TimeoutAsync(TimeSpan.FromSeconds(10)) | |
.WrapAsync(Policy.HandleResult<bool>(r => !r) | |
.WaitAndRetryAsync(3, _ => TimeSpan.FromSeconds(1))) | |
.ExecuteAsync(async () => | |
{ | |
await Task.Delay(1000); | |
return client.ConnectionState == ConnectionState.Connected; | |
}); | |
try | |
{ | |
var currentFilenameFormatting = await client | |
.GetProfileParameter("Output", "FilenameFormatting") | |
.ConfigureAwait(false); | |
logger.LogInformation("Current output file name formatting: {Formatting}", currentFilenameFormatting.ParameterValue); | |
await client | |
.SetProfileParameter("Output", "FilenameFormatting", fileNameFormatting) | |
.ConfigureAwait(false); | |
logger.LogInformation("Updated output file name formatting to: {FolderName}", fileNameFormatting); | |
return Result.Ok(); | |
} | |
catch (Exception ex) | |
{ | |
logger.LogError(ex, "Failed to update OBS folder name."); | |
return Result.Fail(new ExceptionalError("OBS folder name update failed.", ex)); | |
} | |
} | |
} | |
} |
Uses OBS web sockets : https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md
Make sure you enable Web Sockets
Thanks to @tinodo for his C# library obs client. 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use this Program.cs to call the above code