trim and merge 4k video lossless

Post your working scripts, libraries and tools for AHK v1.1 and older
red_sun
Posts: 19
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: 85
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: 6902
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: 19
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.... ;)
SkylarLucas
Posts: 1
Joined: 10 Oct 2022, 17:44

Re: trim and merge 4k video lossless

12 Oct 2022, 04:11

I thought I'd never find this topic. Luckily my specific request is not that specific. Huge thanks for your help, guys. You know, I'm a 53-year-old man and sometimes it's really complicated to find something that would help me on the Internet. That's why I'm extremely grateful for your help. Tbh, I found this thread accidentally. I was looking for how to edit gopro videos and found myself on this page. I guess I'm really lucky today. So, thanks again for your help. Best wishes to you all, guys.
SilvainStiles
Posts: 2
Joined: 30 Oct 2022, 08:01

Re: trim and merge 4k video lossless

30 Oct 2022, 08:02

Hi there! Great code! I love how it is "taylor made" for exactly what you need. I also appreciate how you included a workaround for getting the full filepath from an already-loaded video file. Thank you for sharing!
SilvainStiles
Posts: 2
Joined: 30 Oct 2022, 08:01

Re: trim and merge 4k video lossless

01 Nov 2022, 05:10

By the way, I was looking for a way how to convert mxf video and was wondering what apps you guys use for that.
User avatar
noname
Posts: 516
Joined: 19 Nov 2013, 09:15

Re: trim and merge 4k video lossless

01 Nov 2022, 09:54

If you are looking for an app i would use VLC media player . It is the most versatile and include a converter . The code from this post will convert it to mp4 if you open it in Potplayer . The ffmpeg that is used in it will convert almost anything and the code luckily just get the video filename with extension so it is up to ffmpeg if the conversion works .

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: Bing [Bot] and 105 guests