Python / AtomicParsley script to auto-tag iTunes TV shows

Simple script I wrote today to autotag iPhone shows encoded with Handbrake on my Mac.

Uses Python and AtomicParsley

The filenames have the format: “201 - <episode title>.mp4”

The “201” corresponds to Season “2” Episode “01”.

When the files are imported into iTunes, the Artwork is set, the metadata is parsed for info, and they’re correctly tagged as “TV Shows” instead of the default “Movie”.


#!/usr/bin/env python
	
import sys
from os import listdir, system
from fnmatch import fnmatch
	
# NAME OF SHOW
show_name="Family Guy"
	
# SHOW ARTWORK, LEAVE BLANK IF NO ARTWORK
show_art="S2.jpg"
	
for show in listdir('.'):
	if fnmatch(show,'*.mp4'):
		season='0'+show[0]
		episode=show[1:3]
		show_id='0'+show[:3]
		episode_name=show.split(' - ')[1].split('.')[0]
	
		meta_tags = 'AtomicParsley \"' + show \
	                  + '\" --TVShowName \"' + show_name \
	                  + '\" --TVEpisode \"' + show_id \
	                  + '\" --TVSeasonNum \"' + season \
	                  + '\" --TVEpisodeNum \"' + episode \
		          + '\" --artwork \"' + show_art \
	                  + '\" --stik \"TV Show\" --overWrite'
	
# DEBUG PRINTS, JUST TO CHECK PROPER VARIABLE ASSIGNMENTS	                  
#		print "Show: %s, Season: %s, Episode Number: %s, Show ID: %s" % (show_name, season, episode, show_id)
#		print meta_tags
	
# PERFORM TAGGIN ON FILE
		print "\nProcessing File %s" % show 
		system(meta_tags)
	

Get Info on AVI files in C

Simple way to get the Resolution, Frame Rate, and Run Time of an AVI file in C:


#include <stdio.h>
#include <stdbool.h>
#include <sys/stat.h>
	
bool FileExists (char* FilePath);
	
int main (int argc, char * const argv[]) {
    FILE *fd;
    int timedelay,frames,width,height,HH,MM,SS;
    float framerate,runtime;
    char convruntime[9];
    
    if ((argc >1) && FileExists(argv[1])) {
        fd = fopen(argv[1], "rb");
        
        // Get the Time Delay
        fseek(fd, 32, SEEK_SET);
        fread(&timedelay, 4, 1, fd);
        
        // Number of Frames
        fseek(fd, 48, SEEK_SET);
        fread(&frames, 4, 1, fd);
        
        // Width and Height
        fseek(fd, 64, SEEK_SET);
        fread(&width, 4, 1, fd);
        fread(&height, 4, 1, fd);
	
        fclose(fd);
        
        // Calculate Frame Rate
        framerate = ((1.0/timedelay)*1000000);
        
        // Calculate Runtime in Seconds
        runtime = (frames/framerate);
        
        // Convert Runtime to HH:MM:SS format
        HH= runtime/3600;
        MM = ((runtime-HH*3600)/60);
        SS = (runtime-((HH*3600) + (MM*60)));
        snprintf(convruntime,9,"%02d:%02d:%02d",HH,MM,SS);
        
        printf("Dimensions=%ix%i, Framerate=%.3f, Runtime=%.0f, Length=%s\n",width,height,framerate,runtime,convruntime);
        return 0;
    }else{
        printf("\nUsage:  %s input_file.avi\n\n",argv[0]);
        return 1;
    }
}
	
bool FileExists (char* FilePath) {
  struct stat sb;
  if (stat(FilePath, &sb) != -1)
    return true;
  return false;
}