Last active
January 23, 2022 17:12
-
-
Save ra101/6a0e3bc213c79cefcba2d144c045794d to your computer and use it in GitHub Desktop.
Concat .mp4 files, using ffmpeg and python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Output in same dir as script | |
import os | |
import tkinter as tk | |
from tkinter import ttk | |
from tkinter import filedialog as fd | |
from tkinter.messagebox import showinfo, askokcancel | |
def concat(): | |
filetypes = ( | |
('Video files', '.ts .mp4'), | |
) | |
files = fd.askopenfilenames( | |
title='Open files', | |
initialdir=os.getcwd(), | |
filetypes=filetypes | |
) | |
if (len(files)<1): | |
return | |
output_name = name.get("1.0", "end-1c") or files[0] + '.output.mp4' | |
if not output_name.endswith(".mp4"): | |
output_name +='.mp4' | |
is_ok = askokcancel( | |
title='Please Check the order!', | |
message=f"{files} --> {output_name}" | |
) | |
if not is_ok: | |
return | |
# .mp4 -> .ts, (ignoring already .ts files) | |
temp_files = list(files) | |
for i, file in enumerate(files): | |
if file.endswith(".ts"): | |
continue | |
temp_files[i] = temp_files[i].replace('.mp4', '.ts') | |
os.system(f'''ffmpeg -i "{file}" -c copy -bsf:v h264_mp4toannexb -f mpegts "{temp_files[i]}"''') | |
# concating all the .ts files created | |
os.system(f'''ffmpeg -i "concat:{"|".join(temp_files)}" -c copy -bsf:a aac_adtstoasc "{output_name}"''') | |
showinfo( | |
title='Done!', | |
message=output_name | |
) | |
# deleting all the .ts and .mp4 files except output | |
for i in {*files, *temp_files}: | |
os.remove(i) | |
exit() | |
if __name__=="__main__": | |
# create the root window | |
root = tk.Tk() | |
root.title('Concat MP4') | |
root.resizable(False, False) | |
root.geometry('300x150') | |
# open button | |
open_button = ttk.Button( | |
root, | |
text='Select Files', | |
command=concat | |
) | |
# text box | |
name = tk.Text(root) | |
# place on tk | |
open_button.pack(expand=True) | |
name.pack() | |
# start GUI | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment