Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [

{
"name": "Python Debugger: Current File with Arguments",
"type": "debugpy",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"args": ["C:/Users/s1109/Desktop/Code/python-video-silence-cutter/testdata/simple_video.mkv"]
}
]
}
31 changes: 21 additions & 10 deletions silence_cutter.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ def findSilences(filename, dB = -35):
logging.debug(" words: " + str(words))
for i in range (len(words)):
if ("silence_start" in words[i]):
time_list.append (float(words[i+1]))
time_list.append (float(words[i+1].replace("\\r","")))
if "silence_end" in words[i]:
time_list.append (float (words[i+1]))
time_list.append (float (words[i+1].replace("\\r","")))
silence_section_list = list (zip(*[iter(time_list)]*2))

#return silence_section_list
Expand Down Expand Up @@ -92,25 +92,36 @@ def getFileContent_audioFilter(videoSectionTimings):
ret += "', asetpts=N/SR/TB"
return ret

def writeFile (filename, content):
def writeFile (file, content):
logging.debug(f"writeFile ()")
logging.debug(f" - filename = {filename}")
logging.debug(f" - filename = {file.name}")

with open (filename, "w") as file:
file.write (str(content))
try:
if os.path.exists(file.name):
with open(file.name, mode='w', encoding='utf-8') as f:
f.write(content)
else:
print(f"Can't read file: {file.name} ")
except PermissionError:
print("Permission denied: You don't have the necessary permissions to change the permissions of this file.")
except Exception as e:
print(f"occur error: {e}")


def ffmpeg_run (file, videoFilter, audioFilter, outfile):
logging.debug(f"ffmpeg_run ()")

# prepare filter files
vFile = tempfile.NamedTemporaryFile (mode="w", encoding="UTF-8", prefix="silence_video")
aFile = tempfile.NamedTemporaryFile (mode="w", encoding="UTF-8", prefix="silence_audio")
vFile = tempfile.NamedTemporaryFile (mode="w", encoding="UTF-8", prefix="silence_video", delete=False)
aFile = tempfile.NamedTemporaryFile (mode="w", encoding="UTF-8", prefix="silence_audio", delete=False)

vFile.close()
aFile.close()
videoFilter_file = vFile.name #"/tmp/videoFilter" # TODO: replace with tempfile
audioFilter_file = aFile.name #"/tmp/audioFilter" # TODO: replace with tempfile
writeFile (videoFilter_file, videoFilter)
writeFile (audioFilter_file, audioFilter)

writeFile (vFile, videoFilter)
writeFile (aFile, audioFilter)

command = ["ffmpeg","-i",file,
"-filter_script:v",videoFilter_file,
Expand Down
Binary file added testdata/simple_video_cut.mkv
Binary file not shown.