trim and merge 4k video lossless

Post your working scripts, libraries and tools
red_sun
Posts: 8
Joined: 12 Jul 2019, 05:22

trim and merge 4k video lossless

12 Jul 2019, 05:47

This code relies on potplayer as video editor viewer and ffmpeg as processing software , the free editors I tested were overkill for the simple tasks of trimming and merging.The win10 video editor could trim but not merge . The code is an example because it is "taylor made " for the job I needed ( only max 3 video segments to merge )
ffmpeg can do the job without recoding , it is fast and the filesize does not increase .

Potplayer displayed 4k without delays and provide frame select accuracy using hotkeys. The only thing missing is getting the full filepath from an already loaded video file , if anyone has a solution that would be great .

The workaround is getting the file using the Gui and then starting potplayer with it , potplayer must be the default for the video filetype to work.

The way to use it is to select a video file using the button in the Gui , the mark the begin point on the timeline of potplayer and press " Space " it will send the converted time to the gui and moves the focus to the next editcontrol for the end point of the first segment.

If more segments are choosen you can merge them after trimming.

Code: Select all

setworkingdir , %a_scriptdir% 

/*
Lossless trimming and merging of video fragments ( must be same encoder and format )
The commandwindow stays open to catch errors if any during processing.

ffmeg.exe must be in the same directory as script
for download : https ffmpeg.org /download.html#build-windows  Broken Link for safety

potplayer must be the default app for opening the file
for download :  https potplayer.daum.net /  Broken Link for safety

hotkeys:
spacebar    will set the time from the timeline to the window

thanks to x32 for the sendmessage functions:  https://www.autohotkey.com/boards/viewtopic.php?f=6&t=45385

*/



; ***** clear all existing work files  **** be aware that it will remove any video filename !

filedelete,mylist.txt
FileDelete, video*
filedelete,output.mp4  ; used when no filename is given when merging

gui , +alwaysontop
Gui, add ,edit , section w100 h20 vs1
Gui, add ,edit , ys xs+120 w100 h20 vs2
Gui, add ,edit , y40 xm section w100 h20 vs3
Gui, add ,edit , ys xs+120 w100 h20 vs4
Gui, add ,edit , y80 xm section w100 h20 vs5
Gui, add ,edit , ys xs+120 w100 h20 vs6
Gui, add ,button , y120 xs+120 w100 h60 ggetfile ,select file
Gui, add ,button , y120 xm w100 h20 gtrim ,trim
Gui, add ,button , y160 xm w100 h20 gmerge ,merge
Gui, show,,trimcut
WinMove, trimcut,,% A_ScreenWidth-500,100
return


getfile:
FileSelectFile, svideo,,e:\
Run, %svideo%
sleep 2000
SendMessage,0x0111,20000,,,ahk_class PotPlayer64
Winactivate,trimcut
WinWaitActive, trimcut
ControlFocus, edit1,trimcut
return

merge:
loop %counter%
  data .="file video" A_Index ".mp4" "`n"
FileAppend, %data%,mylist.txt ; create file containing the video fragments to merge
loop 20
  {
  IfNotExist, mylist.txt
    sleep 1000
  }
InputBox, ofile,set outputfile with extension mp4,,,300,180
if !regexmatch(ofile,"\.mp4")
{
ofile:=ofile ".mp4"
msgbox added ext to filename   %ofile%  !
}

if (ofile="")
ofile=output.mp4  ; if no file name is given use this one.....

run , ffmpeg  -f concat -safe 0 -i mylist.txt -c copy %ofile%
return


space::
Winactivate,trimcut
WinWaitActive, trimcut
ControlGetFocus, focus_ , trimcut
controlsettext,%focus_%,% 2time(ppctm()),trimcut
sleep 200
send ,{tab}
return 

trim:

ifnotexist %svideo%
{
msgbox video file " %svideo% " not found or none selected
exit
}
gui ,submit,nohide
time1:={start:s1,stop:s2}
time2:={start:s3,stop:s4}
time3:={start:s5,stop:s6}

if (s2="")
{
msgbox error no first end time
exit
}
if (s4="")
counter:=1
else if (s6="")
counter:=2
else
counter:=3

goto runtrim
return

runtrim:
Loop, %counter%
{
starttime:=time%A_Index%.start
endtime:=time%A_Index%.stop
ovideo=video%A_Index%.mp4

run %comSpec% /k "ffmpeg.exe -i %svideo% -ss %starttime% -to %endtime% -vcodec copy -acodec copy %ovideo% "


sleep 1000
loop 20 ; waiting for the file to exist
  {
  IfNotExist, %ovideo%
    sleep 1000
  }
}
return

ppctm()  ; returns timeline position
		{
		SendMessage, 0x0400, 0x5004,,, ahk_class PotPlayer64
		Return, %ErrorLevel%	
		}
		
2time(x){  ; convert potlayer time format to ffmpeg format
RegExMatch( x,"U)^(\d+)(\d\d\d$)",m)
    time = 19990101  
    time += %m1%, seconds
    FormatTime, mmss, %time%, HH:mm:ss
    if m2
    return mmss "." m2
    else
    return mmss
}

guiclose:
ExitApp, 
User avatar
davebrny
Posts: 83
Joined: 05 Dec 2016, 06:26

Re: trim and merge 4k video lossless

13 Jul 2019, 15:38

red_sun wrote:
12 Jul 2019, 05:47
The only thing missing is getting the full filepath from an already loaded video file , if anyone has a solution that would be great .
you could send F3 which would open up the folder that the file is in, then send ^l and ^c to get the folder path from the address bar. not the cleanest solution but im not sure there would be any other way
User avatar
jeeswg
Posts: 6471
Joined: 19 Dec 2016, 01:58
Location: UK

Re: trim and merge 4k video lossless

13 Jul 2019, 16:27

- To get the video path, I might:
- Get the window title via WinGetTitle, perhaps setting it to be the name/full path.
- Compare the name from the window title with shortcuts in the Recent folder, using FileGetShortcut.
- Perhaps recently played files are stored in the registry, or somewhere else, so RegRead.
- There might be some option to put the path onto the clipboard (so, the Clipboard variable), or create a playlist file, and use FileRead.
- You could see if AccViewer can get it, and then use the Acc functions.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
red_sun
Posts: 8
Joined: 12 Jul 2019, 05:22

Re: trim and merge 4k video lossless

14 Jul 2019, 10:06

Thanks for the suggestions , the path info is set as a OSD layered on the video as an option but I could not capture it . Purely serendipitous I found that f12 opens a small navigation window that has the current video path/filename as part of the title . It is not a "clean" way but still an easy one.... ;)

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 36 guests