Play videos from a folder at a certain time

Hello,

I would really appreciate your kind help with the next situation I have.

I have many videos that are classified in many folders.
Duration of each video is around 1 hour.
Different formats: mkv, mp4, avi and wmv.

I need to play all the videos of a folder with VLC app at certain 3 interval fixed times:
5 minutes, 30 minutes and 50 minutes with duration of 5 seconds of each interval, and then pass to the next video until the last one.

It would be helpful the script ask something like:
Select the folder.

In reference of the videos: they are in folders in different external hard disks.

Sorry for my bad english.

Appreciated your kind help.

Model: mac mini 2012
AppleScript: 2.7
Operating System: MacOS Catalina (10.15.7)

One thing that comes to my mind is to use VLC command-line options:

tell application "Finder"
	-- get path to VLC binary
	set vlc to quoted form of (((POSIX path of (path to application "VLC")) as text) & "/Contents/MacOS/VLC")
	-- select folder to process
	set moviedir to (choose folder with prompt "Choose movie folder:")
	-- get all movies inside the selected folder
	-- I'm assuming you can't rely on mdfind because some of those external disks
	-- might be indexed and some may be not. That's why I'm only checking if
	-- file name ends with ".mp4"
	set filelist to files of moviedir
	repeat with moviefile in filelist
		if (moviefile as text) ends with ".mp4" or (moviefile as text) ends with ".mkv" or (moviefile as text) ends with ".avi" or (moviefile as text) ends with ".wmv" then
			-- vlc command-line options:
			-- --start-time <seconds>
			-- --stop-time <seconds>
			-- vlc://quit quits after playing the file
			do shell script vlc & " --start-time=300 --stop-time=305  --video-on-top " & quoted form of (POSIX path of (moviefile as alias)) & " 'vlc://quit'"
		end if
	end repeat
end tell
1 Like

VLC is incredibly flaky as a scriptable application but this may work… occasionally.

The first part generates a list of file urls for VLC to open. They take the form:

file:///Users/username/Movies/video - 01.mp4

Note the three slashes after the colon. The first two are part of setting the protocol and the third is part of the path to the video. You’ll have to take this into account when the files are on an external drive. You can see a video’s path when it is playing by opening Windows > Media Information… it should be at the bottom of the General tab in the Location field.

The VLC part ‘opens’ each file by adding it to the playlist. It then begins to play the video from the beginning. After some portion of a second, it skips to the 5 minute (and then 30 and 50) mark and plays for around 5 seconds. Part of the flakiness I see is that it will ignore the skip to 5 minutes command and just begin playing at the start, regardless of what it told to do. Setting the delay to 1 second seems to be the least problematic approach to getting it to skip ahead.

It cycles through each video (hopefully) and does the same with each. I know that there are two stop commands. That’s just how it goes.

Note that the playlist should be empty when the script is run.

set cf to choose folder
set pcf to POSIX path of cf

tell application "System Events"
	set fil to every file of cf whose visible is true
	set vidTypes to {"mp4", "mkv", "avi", "wmv"}
	set vList to {}
	repeat with x in fil
		if name extension of x is in vidTypes then
			set y to POSIX path of x
			set end of vList to ("file://" & y)
		end if
	end repeat
	vList
end tell

set x to length of vList
tell application "VLC"
	activate
	set v to 0
	repeat x times
		set v to v + 1
		set vid to item v of vList
		OpenURL vid
		next
		set current time to 0
		if playing is false then
			play vid
		end if
		delay 1
		
		set current time to 300
		if playing is false then
			play vid
		end if
		delay 5
		
		set current time to 1800
		if playing is false then
			play vid
		end if
		delay 5
		
		set current time to 3000
		if playing is false then
			play vid
		end if
		delay 5
		stop
	end repeat
	stop
end tell

Depending upon how smoothly this needs to run, I might look at using ffmpeg (or other tools) to slice and dice the video collection as required and make a single video that includes the three excerpts from each video.

2 Likes

kmitko
Mockman
Thank you very much for your kind help.

kmitko code works but only play the first interval, not the second and the third one, for all the videos in the folder.

Mockman your script does what I exactly need. I was testing it.
With some folders works well. But with others, it doesn’t play all the files.
Some times it plays the videos in a disordered way (not in alphabetical order).

Is there any chance your script let me load the playlist from a text file?

Something like:
Select your movie folder?
Select your vList?

Following your advice my vList.txt would be:
file:///Volumes/HDD01/CASAMENTOS/SALAO/casal.mp4
file:///Volumes/HDD01/CASAMENTOS/SALAO/bolo.mkv
file:///Volumes/HDD01/CASAMENTOS/SALAO/flores.wmv
file:///Volumes/HDD01/CASAMENTOS/SALAO/recepcao.avi

Appreciate your help.
thank you.

There are a few ways that you could do that.

This will ask you to choose a folder and then use the file inside called ‘vList.txt’. Alternatively, you could replace that line with a choose file command. I used the ‘posix’ format for referencing files here just because it more closely matches the references that VLC seems to prefer but it is optional.

set movieFolder to choose folder
set mf to POSIX path of movieFolder

-- vList.txt that's within movieFolder
set vFile to mf & "vList.txt"

set vList to read POSIX file vFile as «class utf8» using delimiter linefeed
--> {"file:///Volumes/HDD01/CASAMENTOS/SALAO/casal.mp4", "file:///Volumes/HDD01/CASAMENTOS/SALAO/bolo.mkv", "file:///Volumes/HDD01/CASAMENTOS/SALAO/flores.wmv", "file:///Volumes/HDD01/CASAMENTOS/SALAO/recepcao.avi"}

Easiest though, would simply be to choose the file and not worry about the folder.

set vFile to (choose file)
set vList to read vFile as «class utf8» using delimiter linefeed

It is possible that if some of your files have accented characters in them, that they may not be read correctly. If that happens, you might need to have it read the file while looking for a different encoding than utf-8.

1 Like

Mockman, thank you very much.

According to your suggestions I changed your original script as below but it doesn’t work.
VLC says: “unable to open the MRL”

set vFile to (choose file)
set vList to read vFile as «class utf8» using delimiter linefeed

set x to length of vList
tell application "VLC"
	activate
	set v to 0
	repeat x times
		set v to v + 1
		set vid to item v of vList
		OpenURL vid
		next
		set current time to 0
		if playing is false then
			play vid
		end if
		delay 1
		
		set current time to 300
		if playing is false then
			play vid
		end if
		delay 5
		
		set current time to 1800
		if playing is false then
			play vid
		end if
		delay 5
		
		set current time to 3300
		if playing is false then
			play vid
		end if
		delay 5
		stop
	end repeat
	stop
end tell

Anyway, it was not a good idea of mine to add a list txt file that I thought would ease the problem of not playing some videos or not reading them in alphabetical order.

After thinking about it carefully, I like your first original script is much simpler and easier.
I only cannot understand why the script doesn’t play some videos or plays them out of order.

The names of my files are plain. They do not have accents or especial characters.

Thank you very much for any suggestion.

I’m not sure I can be of help here. I took your version of the script and substituted in some paths on my own system and it worked perfectly.

One small possibility… try changing the word ‘linefeed’ to ‘return’. If your document uses the return character instead of the linefeed, then that would affect how the paragraphs were split up (or not split up).

Otherwise, try changing the file names by putting a number at the beginning of each file, with the order that you wish the videos to play in, eg 1casal.mp4, 2bolo.mkv and then try the version that makes a list from the items in the folder. Perhaps that will help that version play them in order. By the way… when it doesn’t play a video, is it any particular file type? eg won’t play ‘wmv’?

The last thought I have is that VLC is not liking the way the path is written for the external drive. Two things you could try… confirm that the format is the same in the text file as it is in the media information window, and copy the videos to the computer’s internal drive, enter the new path information into the text file and try with that.

1 Like

This was it.
Thank you very much for your kind help, Mockman.

Sometimes, the smallest things can get in the way. I’m glad it worked out for you.

1 Like