DVD filler

Podcasts are great but they tend to take a lot of room if you don’t tidy up every now and again.¬†This short script in Ruby fills up a DVD from a folder full of mp3’s (typically podcasts). The mp3’s are assumed to be stored in different folders, the script goes through them all, selects the oldest ones, moves them to a destination directory while keeping the folder names intact and creating local playlists.

The end-result is a directory the size of a DVD, ready to be burned, consisting of the oldest podcasts you have accumulated.

Select All Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/ruby
#class PodcastLibrary
 
require 'fileutils'
 
class PodcastLibrary
def initialize
	# initialize array
	@library=[]
end
 
def addPodcast(path,folder,title,size,date)
	podcast={
		"Path"   => path,
		"Folder" => folder,
		"Title"  => title,
		"Size"   => size,
		"Date"   => date
	}
	@library.push(podcast)
end
 
def to_s
	s=""
	@library.each {|podcast|
		s+= podcast["Folder"]+"/"+podcast["Title"]+" ("+podcast["Size"].to_s+") on "+podcast["Date"].to_s+"n"
	}
 s
end
 
def sortByDate
	@library=@library.sort {|p1,p2| p1["Date"]<=>p2["Date"] }
end
 
def populate(path) # populate the library with the content of a directory
Dir.foreach(path){|f|
	dirname=File.new(path+"/"+f,"r")
	if dirname.lstat.directory? then
	next if f == "." or f == ".."
	Dir.foreach(path+"/"+f) {|sfile|
	if sfile =~ /.(mp3|ogg)$/ then
		podcast=File.new(path+"/"+f+"/"+sfile).stat
		size=podcast.size
		folder=f
		title=sfile
		date=podcast.mtime
		self.addPodcast(path,folder,title,size,date)
		end
	}
	end
}
 
self.sortByDate
end
 
def pop # delete last podcast
@library.pop
 
end
 
def makeList(maxSize=1024*1024*1024*4.3) # select the first (by date) podcasts so as to fill up a DVD
dvd=PodcastLibrary.new
total=0
@library.find {|p|
	dvd.addPodcast(p["Path"],p["Folder"],p["Title"],p["Size"],p["Date"])
	total+=p["Size"]
	total>maxSize}
 
dvd.pop
dvd
end
 
def count # number of podcasts in the library
@library.length
end
 
def size # total size of the library
total=0
@library.each {|p| total+=p["Size"]}
total
end
 
def move(destination="/home/YOURDEFAULTDESTINATION/DVDPodcasts") # move the library to destination and create the playlists
@library.reverse! # better for playlist, esp. for series
globalplaylist=File.new(destination+"/playlist.m3u","w+")
@library.each {|p|
	# create folder if necessary
	FileUtils.mkdir destination + "/" + p["Folder"] unless File.exist?(destination+"/"+p["Folder"])
	# update the local playlist
	playlist=File.new(destination+"/"+p["Folder"]+"/playlist.m3u","a+")
	playlist.puts(p["Title"])
	playlist.close
 
	# update the global playlist
	globalplaylist.puts(p["Folder"]+"/"+p["Title"])
 
	# move the file
	origfile=p["Path"]+"/"+p["Folder"]+"/"+p["Title"]
	destfile=destination+"/"+p["Folder"]+"/"+p["Title"]
	# FileUtils.cp(origfile,destfile)
	# puts "copying: "+p["Folder"]+"/"+p["Title"]
	FileUtils.mv(origfile,destfile)
	puts "moving: "+p["Folder"]+"/"+p["Title"]
	FileUtils.rm origfile, :force => true
 
}
 
globalplaylist.close
@library.reverse!
end
 
end
 
lib=PodcastLibrary.new
path="/home/WHEREYOURPODCASTSARESTORED/podcasts"
lib.populate(path)
lib2=lib.makeList
#puts lib2
#puts lib2.size
lib2.move

Comments are closed.