Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save BarkBarklington/5ed80bf28cac216438fbc642ce36e937 to your computer and use it in GitHub Desktop.

Select an option

Save BarkBarklington/5ed80bf28cac216438fbc642ce36e937 to your computer and use it in GitHub Desktop.
Sketch ass script to convert the m4s files being saved by the new Steam Game Recording feature into an mp4 file. It also allows "clipping" the last 30 seconds of the most recent active background recording.
import os
import subprocess
from random import sample
from uuid import uuid4
from pathlib import Path
def create_filelist(folder, init_file, file_pattern):
files = sorted([f for f in os.listdir(folder) if f.startswith(file_pattern) and f.endswith('.m4s')])
return [f"{os.path.join(folder, init_file)}"] + [f"{os.path.join(folder, file)}" for file in files]
def concatenate_files_with_cat(files, output_file):
with open(output_file, 'wb') as out_file:
for file in files:
with open(file, 'rb') as in_file:
out_file.write(in_file.read())
def merge_audio_video(video_file, audio_file, output_file):
command = [
'ffmpeg', '-y', '-i', video_file, '-i', audio_file,
'-c', 'copy',
'-analyzeduration', '100M', '-probesize', '50M',
output_file
]
subprocess.run(command, check=True)
def decode_video(folder, output_filepath, num_last_files_to_save=0):
uid = uuid4()
video_combined = f'/tmp/{uid}_video_combined.mp4'
audio_combined = f'/tmp/{uid}_audio_combined.mp4'
final_output = output_filepath
# Assuming init and chunk files have a consistent naming convention
video_init = 'init-stream0.m4s'
video_pattern = 'chunk-stream0'
# Create file lists
video_filelist = create_filelist(folder, video_init, video_pattern)
if num_last_files_to_save:
video_filelist = [video_filelist[0]] + video_filelist[1:][-num_last_files_to_save:]
audio_filelist = [f.replace("stream0", "stream1") for f in video_filelist]
# Concatenate files using cat
concatenate_files_with_cat(video_filelist, video_combined)
concatenate_files_with_cat(audio_filelist, audio_combined)
# Merge audio and video using ffmpeg
merge_audio_video(video_combined, audio_combined, final_output)
# Clean up
os.remove(video_combined)
os.remove(audio_combined)
def get_most_recent_bg_recording_folder():
path = sorted((Path.home() / ".local/share/Steam/userdata").glob("**/bg_*/"), key=os.path.getmtime, reverse=True)
if not path:
return None
return path[0]
if __name__ == "__main__":
# Example usage
folder = get_most_recent_bg_recording_folder()
decode_video(folder, "/tmp/output.mp4", num_last_files_to_save=10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment