We automatically detect ad creative formats (e.g. 1x1, 9x16, 4x5) from filenames, normalize them, and group related media files together (e.g. feed + story + square versions of the same ad).
Core Flow:
- Detect format — using 20+ regex patterns for common naming conventions (_9x16, (Feed), -S, etc.).
- Normalize filename — clean suffixes, unify separators, lowercase everything, and strip format tags.
- Group files — combine variations (e.g. feed, story, square,reel) into sets of 2–3 files per group, ensuring unique formats.
- Classify format — map dimensions or codes into categories:
- square → 1x1
- portrait → 4x5
- vertical → 9x16
- reel → 9x16
- landscape → 16x9
- Validate — enforce group size (2–3), unique formats, and mixed categories (not all vertical).
- Detect file type — based on file extension (image vs video).
Goal:
Ensure consistent media grouping and reliable automated creative uploads for multi-placement ad campaigns.
Filename Normalization Rules
Pre-Processing
- Upload Suffix Removal: Remove 4-character upload suffixes (e.g.,
_btLP,_ijCA) before format detection- Exception: Keep if it's a meaningful word:
main,test,prod,beta,demo,live,sale - Exception: Keep if all letters and same case (likely a word, not random suffix)
- Exception: Keep if it's a meaningful word:
Version/Variant Preservation
- Version Suffix Preservation: Preserve date-like version patterns (e.g.,
250905-6,_250905_3)- Normalization: Convert underscores to hyphens
- Purpose: Keep different versions separate
- Trailing Number Preservation: Preserve trailing variant numbers before extension
- Purpose: Keep different numbered variants separate
Format Removal
- Primary Format Removal: Remove the detected format string
- Additional Format Cleanup:
- Remove explicit format dimensions:
_4x5,_9x16(before extension) - Remove format dimensions in parentheses:
(4X5),(9X16) - Remove format dimensions with 4-char hash:
_4x5_ZKce,_9x16_ntv1
- Remove explicit format dimensions:
- Upload Suffix Removal (Post-Format):
- Remove 4-character upload suffixes after format removal
- Same exceptions as pre-processing (meaningful words, same-case words)
Special Handling
- Instagram Story/Post: Don't remove the number from Instagram story/post patterns (needed for grouping)
Normalization
- Separator Normalization:
- Unify various separators to hyphens
- Collapse multiple hyphens to single hyphen
- Remove leading/trailing hyphens
- Case Normalization: Convert to lowercase
- Extension Handling:
- Ensure extension is present and lowercase
- Remove trailing dots
- Fallback: If name becomes empty or just extension, create fallback from original name
- Re-append Preserved Elements:
- Re-append version suffix if preserved
- Re-append trailing number if preserved (normalize separator to hyphen)
Grouping Rules
Group Key Generation
- Placement-Based Files:
- Detection: Files with
feed,story, orstorieskeywords with separators/spaces - Group Key: Remove placement keywords, normalize separators
- Purpose: Group files like
thebest_feed.pngandthebest_stories.pngtogether
- Detection: Files with
- Platform-Prefixed Files:
- Detection: Files starting with
instaorfacebookprefix - Group Key: Remove platform prefix, keep trailing numbers
- Purpose: Group files like
Insta content 1_9x16,Insta content 1_4x5ascontent-1
- Detection: Files starting with
- Standard Format-Based Grouping:
- Group Key: Normalized base name (format removed, normalized)
Group Assignment Rules
- Type Matching: Items must have the same type (image or video) to be in the same group
- If types don't match, create new group with counter suffix
- Maximum Group Size: Maximum 3 items per group
- If group is full, create new group with counter suffix
- Format Uniqueness: Each format can only appear once per group
- If format already exists in group, create new group with counter suffix
- Unique Key Generation: Use
previewURL, fallback tourl, fallback toname_index
Format Normalization Rules
Short Code Mapping
- Square Formats:
11,1x1,sq,g→square
- Vertical Formats:
916,9x16,vt,story,s→vertical
- Landscape Formats:
169,16x9→landscape
- Portrait Formats:
45,4x5,fi,feed,infeed→portrait
Dimension Mapping
- Common Pixel Dimensions:
1080x1080→square1080x1920→vertical1080x1350→portrait1080x1440→portrait(3:4 ratio)1920x1080→landscape1200x628→landscape628x1200→vertical1350x1080→landscape
Ratio-Based Classification
- Aspect Ratio Rules (for any
NxNformat):- Square:
ratio ≈ 1(0.9 to 1.1) - Vertical:
ratio ≤ 0.7(e.g., 9:16 = 0.56) - Portrait:
0.7 < ratio < 0.9(e.g., 4:5 = 0.8) - Landscape:
ratio ≥ 1.5(e.g., 16:9 = 1.77) - Default Landscape:
1.1 ≤ ratio < 1.5(e.g., 4:3 = 1.33)
- Square:
Format Conversion
- Separator Normalization: Convert
N_N,N:N,N-NtoNxN
Validation Rules
Group Size Validation
- Minimum Size: Group must have at least 2 items
- Maximum Size: Group must have at most 3 items
Format Validation
- Format Detection: All items must have a determinable format
- Format Uniqueness: Each item must have a different format (no duplicates)
- Format Count: Must have at least 2 different valid formats
Category Validation
- Category Requirements:
- Special Case: Square + Portrait/Vertical + Landscape combination is always valid
- General Rule: Must have formats from at least 2 different categories
- Categories:
portrait,vertical,square,landscape
- Category Validation: Formats must belong to more than one category
- All items cannot be the same category (e.g., all vertical)
File Type Detection Rules
Image Extensions
png,jpg,jpeg,gif,webp,heic,heif,svg,bmp,tiff
Video Extensions
mov,mp4,m4v,avi,mkv,webm,flv,wmv,mpeg,mpg
Return Values
- Returns
'image','video', or'unknown'based on file extension
Notes
- Pattern matching is done on both cleaned and original filenames
- Patterns are evaluated in order (highest priority first)
- Grouping uses normalized base names to create group keys
- Format normalization converts various formats to standard categories:
square,vertical,portrait,landscape