How to create a movie database starting from a list of files

前端 未结 1 587
北恋
北恋 2021-01-27 06:29

I have a large amount of movies on my home server (4000 circa). The files are all named Title - Subtitle (year).extension. I would like to create a database (even i

相关标签:
1条回答
  • 2021-01-27 07:11

    This is a pretty broad question and not really appropriate here (this is more of a tutorial than a quick code question), but here's some strategic advice:

    • Excel will open a .csv and treat commas/new lines as cells. So
    • You need to iterate, maybe recursively, over the directory(ies)
    • Expand the path name—if you use a high-level language like Python, this is acheived by standard functions; then use regular expressions to parse the final bit
    • Store the formatted contents of each path as rows in a list
    • Print that list to a text file, joining each element by commas and each row by a new line character
    • Provide said file with a .csv suffix and open it in Excel

    Note that if you really want a database proper, Python again is a nice choice—SQLite is part of the standard install.

    Cheers, good luck


    UPDATE: Haha, you edited the question whilst I answered. It seems like everything you need is in the file name, but if you're planning on using metadata, here's a caution. Pulling the metadata out of your files can get trickier if they've not all come from the same source; not every media type has the same metadata structure, not every application that creates the files provides the same. So the logic of getting your metadata can get messy.

    Is there a reason you can't use extant programs to do this?

    Finally you mention getting it on your web-server; once again deferring to Python, the capacity to make the requests of your server you need is also built into the standard package.


    Final Update

    Can't help you with bash; I'm all thumbs there, and I'm no expert in Python either but your goals are pretty simple. I haven't tested this—there is probably a typo or two, consider it pseudo-code that is mostly python-ready.

    # import the standard libraries you'll need
    import os # https://docs.python.org/2/library/os.html
    import re # https://docs.python.org/2/library/re.html
    
    # this function will walk your directories and output a list of file paths
    def getFilePaths(directory):
        file_paths = []
        for root, directories, files in os.walk(directory):
            for filename in files:
                filepath = os.path.join(root, filename)
                file_paths.append(filepath)
        return file_paths
    
    
    
    video_file_paths = getFilePaths("path/to/video/library")
    output_to_csv = [];
    for video_file in video_file_paths:
        base_path, fname = os.path.split(video_file) 
    
         """ This is a super simple bit of regex that, provided  your files are all formatted as
         written, will parse out title, subtitle, year and file extension. If your file names
         turn out to have more exceptions than you expect (I'd be shocked if not), you may need
         to make this part more robust, either with much more savvy regex, or else some conditional
         logic—maybe a recursive try... catch loop"""
        reg_ex = re.compile("/^(.*) - (.*) \((.*)\)\.(.*)$/");
    
        # now apply the compiled regex to each path
        name_components = reg_ex.match(fname);
    
        """Each output is a row of your CSV file; .join() will join the 4 elements of the regex
        match (assuming, again, that your filenames are as clean as you claim), and then add
        the basepath, so you should be building, in this loop, a list with elements like:
        title, subtitle, year, file_extension, full path"""
    
        output_to_csv.append("{0},{1}".format(name_components.join(","), base_path));
    
    #create the file, making sure the location is writeable
    csv_doc = open("my_video_database.csv", "w");
    
    # now join all the rows with line breaks and write the compiled text to the file
    csv_doc.write( ouput_to_csv.join("\n") ); 
    
    #close  your new database
    csv_doc.close()
    
    0 讨论(0)
提交回复
热议问题