Posts tagged #programming

Videos Courses - Calculate Total Duration

Paul Solt Swift iPhone App Expert

I record a ton of videos on iPhone app programming, and I didn't have a good way to get the total time for a course from a set of videos.

I previously used VLC, but that gets out of hand when you have 30+ videos to manually calculate total duration.

Day 1 videos are just the beginning

Day 1 videos are just the beginning

I wrote a script in Python, since the didn't seem to work in Swift. Maybe next time it'll be Swift.

Run this code in Terminal and it'll calculate the lengths of all the .mp4 videos and output them to the console. It doesn't work with other video types, but you can grab the code and tweak it. I export all my videos with handbrake (normal settings), which gives me .mp4 video files that are small.

As of now, I have 101 videos from the first 14 days of my Swift iPhone programming course. Using this script I have a total of 12 hours 32 minutes of content.

Run the code

Open Terminal on Mac using Spotlight (Command + Spacebar) > type Terminal > Press enter

python VideoDurationCalculator.py /path/to/your/video/folder

Example:

python VideoDurationCalculator.py /Users/Shared/Course\ Videos/1\ -\ Swift\ in\ 31\ Days/Swift1\ -\ Export\ 1080

VideoDurationCalculator.py

import os
import commands
import sys

# Paul Solt - get the length of all videos within a folder (recursive)
#
# Depending on the number of files/folders, it can take time to complete
#(100 files ~2.018 seconds to run)

# Set your video folder path here
defaultPath = "/Users/Shared/Course Videos/1 - Swift in 31 Days/Swift1 - Handbrake 1080"


def main():
    path = defaultPath

    if len(sys.argv) == 2:
        path = sys.argv[1]
    else:
        print "Notice: Using default path:", defaultPath, "\n"

    #test path
    if os.path.exists(path):
        # Grab all the video (.mp4) files recursively for a folder
        filenames = []
        for root, dirs, files in os.walk(path):
            for file in files:
                if file.endswith(".mp4"):
                    filenames.append(os.path.join(root, file))

        #Calculate the total time of all videos in folder
        totalTimeInSeconds = 0
        print "Calculating video lengths..."
        for file in filenames:
            # Calculate total time
            lengthInSeconds = videoLength(file)
            totalTimeInSeconds += lengthInSeconds

        hours = int(totalTimeInSeconds / 3600)
        minutes = int((totalTimeInSeconds - (hours * 3600)) / 60)
        print "hours:", hours, "Minutes:", minutes
    else:
        print "Error: No folder found with path:", path

# Parse the plist format from mdls command on Terminal
# @return the time in seconds
# mdls -plist - filename.mp4
def videoLength(filename):

    myCommand = "mdls -plist - "
    myCommand = myCommand + "\"" + filename + "\""

    status, plistInput = commands.getstatusoutput(myCommand)

    videoLength = 0
    searchTerm = "kMDItemDurationSeconds"

    # 1. find "kMDItemDurationSeconds"
    # 2. grab text until next 

    realStart = ""
    realEnd = ""

    index = plistInput.find(searchTerm)

    # Offset by length of search term
    startIndex = index + len(searchTerm)

    # Calculate new start/end positions
    startIndex = plistInput.find(realStart, startIndex)
    endIndex = plistInput.find(realEnd, startIndex)

    #offset start by length of realStart text
    startIndex += len(realStart)

    if startIndex != -1 and endIndex != -1:
        #Split and parse as number
        numberString = plistInput[startIndex:endIndex]
        numberString = numberString.strip()
        videoLength = float(numberString)
    else:
        print
    return videoLength

# Run the main method
main()

Connect

  • Do you have a better approach?
  • Subscribe to my mailing list and get a free Swift video course.
  • I'm teaching an online Swift iPhone app course.
Posted on October 23, 2014 and filed under Tips, Programming .

iPhone app, iPhone game, and Swift Programming Courses for iOS 8

Swift iPhone Apps are the Future

Do you want to learn how to make iPhone apps?

Have you heard about Apple's new ?

Do you want to make iPhone apps or iPhone games for iOS 8 and the next iPhone 6?

iPhone Course Bundle

I'm launching on October 6th, 2014.

Swift is a modern, fast, and safe programming language. It will help you create apps faster and write code that doesn't crash. It's replacing Objective-C and you need to learn it now.

Sign up to get access to the new iPhone app programming courses that . You will learn how to make games with Sprite Kit, and how to .

on the retail price of $99/course. 

Stay Informed

Signup for the Swift mailing list for additional app programming resources.


Posted on August 18, 2014 and filed under News .

Swift - Upcoming Programming Language for iPhone and Mac Apps!

Swift Programming Language

Apple just announced a new programming language called Swift. It's a future game changer, but it shouldn't effect your ability to learn how to create and ship iPhone apps today. I just submitted my first Mac app yesterday, but you can't do that with Swift today.

Swift won't be released until Fall 2014. Until then if you want to submit your first app to the App Store you need to start learning to program. Objective-C is the language that you should start with because it's mature and there is a lot of support on the web and videos to learn from. Programming is hard and it will take time to master, but you'll be in better shape if you start now. Apple has been working on some killer features that are making app development even easier, but we'll have to wait to take full advantage of those features.

Stick to Objective-C Until iOS 8 and Xcode 6 is Public

Programming is more than a language itself. Developers create collections of code files called frameworks, and we use these everyday on iPhone and Mac. The way you work with a framework isn't changing, so all of the skills that you learn with Objective-C directly translate to Swift. Rather than put off your dream of making an app you should start now.

I've worked for both Apple Inc. and Microsoft. At both companies we dogfood the latest software, and it's buggy. If you're a beginner the bugs and issues that come with beta software can be a real deal-breaker. Waiting for 4-5 beta releases or until Fall 2014 will make the learning experience a lot easier.

Swift Sneak Peak

I did explore a little bit of Swift and the Xcode Playground editor. It's neat, but auto-complete wasn't working for me. Using Objective-C code is still faster right now unless you memorize the method names and new API. Autocomplete is a tool that suggests things to you so that you can write code correctly and do it fast!

You can take a sneak peak with these resources on Swift. Apple published a programming guide on iBooks, and the Xcode 6 editor includes interactive documentation (live code). If you're adventurous you can install Xcode 6 Beta side-by-side with Xcode 5.

Official Documentation

  • Swift Programming language
  • Xcode 6

Resources

Make your iPhone Apps Today!

If you want to ship something today I would stick to Objective-C. Checkout my online courses for beginner iPhone programming and intermediate iPhone app programming.


Posted on June 3, 2014 and filed under News, Programming .