Time to finally get on with my media optimization; I’ve confirmed my optimization scripts work for the images, gifs, and videos by tediously looking through all of the ouputs. And just to be extraaaa safe I also made a backup. I replaced my project media with this output and then did a batch search through my project entries to replace all “.jpg” and “.png” with “.webp” and all “.mp4”, “.mov”, “.avi”, and “.gif” with “.webm”.
Now, in the age of today I’d be a very silly fool if I designed this system so that every time I upload new media to my project, I have to manually run the media conversion scripts and edit all old references in all of my files—I can design an agent to do this for me.
However… it’s easy to get carried away by this exciting AI stuff, delegating as many tasks as I possibly can to agents, but I need to remember that “Models cannot be treated as reliable computing systems” and consider which parts of this process can just be scripted. For example, I think the renaming of old references can be accomplished via regex, the backup of old media can be scripted, the conversion and replacement of files can be scripted…Come to think of it, this can all be scripted and I don’t need an agent whatsoever! This is why we think things through and plan.
Here’s how I engineered my prompt in Cursor:
Prompt
@convert-images.js @convert-videos.js I have these two scripts that i was using to manually convert all media in my /content/media directory into .webp for images and .webm for videos and gifs. It’s a tedious process that can be optimized because aside from needing to configure the i/o directories for the scripts, i also need to find all references to the original media files in the markdown files my /content/entries and replace those file names with the new file names. For example, if /entries/[date].md had a reference to “Example_1.jpeg”, I would manually change the content of that file to reference “Example_1.webp”. I can use regex and batch searching to speed up the process, but I think we can automate all of this. I want to make it so that whenever i build my quartz project with npm run build, before build we convert all unconverted media, backup any converted media to the /media-backup folder, replace the uncoverted files in my /content/media directory with the converted ones, and then do a regex to replace all obsolete image references in the markdown entries with .webp and all video references with .webm as i described before. To do this on build, we may need to edit the @build-and-optimize.mjs script because i think that gets run on build as well. Refer to my @package.json . I want to be clear that the two conversion scripts don’t yet replace any of the converted media; they just take the input in one directory and output the converted files to a new directory. Therefore, we’ll need to edit the scripts so that they backup the converted files as described and replace the converted files inside the media directory.
Had to provide a few error logs in response because of ES module issues but this worked! The next thing I’d like to do is rename all the media files to match the entry they’re used in. The unorganized file names are just a conflict waiting to happen, especially considering the conversion factor: What if I already have image.webp
and I try to convert image.jpg
?
Ok, actually before I do that, there’s a lower-hanging fruit for me to snag first—I need to sync my converted media to Drive via rClone after it gets converted during build.
🎉 Media conversion process completed successfully!
💾 Original files have been backed up to /media-backup
📝 Markdown files have been updated with new file references
✅ Media conversion completed.
🔄 Syncing converted media files...
✅ Media sync completed.
That was easy. Although I’m glad I paid attention because Claude tried to put the media sync step before the conversion.
There’s another thing I’d like, which is to run build before any git commit to this repo. I’ll need to setup pre-commit hooks with husky before I can achieve this, but I’m gonna call it quits for tonight.
Tags: optimization scripting file-management build devops prompting