Skip to content

Instantly share code, notes, and snippets.

@acegiak
Last active May 20, 2023 05:56
Show Gist options
  • Select an option

  • Save acegiak/295d1f9d07f067060a8e2fb6360a7232 to your computer and use it in GitHub Desktop.

Select an option

Save acegiak/295d1f9d07f067060a8e2fb6360a7232 to your computer and use it in GitHub Desktop.
Split video by silence and upload to Youtube
#!/usr/bin/python
# Written for Ubuntu 16.10
# Requires:
# ffmpeg - https://www.ffmpeg.org/
# youtube-upload - https://github.com/tokland/youtube-upload
# usage: vidprocess.py "filename.mp4" "Title of video - Part " "Description of video." "comma,separated,list of tags,for youtube"
# Make sure to set up youtube-upload with a credentials file first. Script currently assumes secrets file is located at ~/.scripts/youtube/client_secret.json
import subprocess,sys,shlex,re
print "to process: "+sys.argv[1]
findsilence = "ffmpeg -i \""+sys.argv[1]+"\" -filter_complex \"[0:a]silencedetect=n=-90dB:d=0.3[outa]\" -map [outa] -f s16le -y /dev/null"
process = subprocess.Popen(shlex.split(findsilence), stdout=subprocess.PIPE, stderr=subprocess.STDOUT,universal_newlines=True)
(output, err) = process.communicate()
print output
sections = re.findall('silence_end: ([0-9\.]+).*?silence_start: ([\-0-9\.]+)',output,re.DOTALL|re.MULTILINE)
print sections
count = 1
for section in sections:
print section
breakparts = "ffmpeg -ss "+str(float(section[0])-1)+" -t "+str(float(section[1])-float(section[0])+1)+" -i \""+sys.argv[1]+"\" -strict -2 \""+sys.argv[2]+str(count)+".mp4\""
print breakparts
breakprocess = subprocess.Popen(shlex.split(breakparts))
(output, err) = breakprocess.communicate()
print output
options = ""
if len(sys.argv) > 3:
options = options+" --description=\""+sys.argv[3]+"\""
if len(sys.argv) > 4:
options = options+" --tags=\""+sys.argv[4]+"\""
upload = "youtube-upload --title=\""+sys.argv[2]+str(count)+"\""+options+" --client-secrets=~/.scripts/youtube/client_secret.json \""+sys.argv[2]+str(count)+".mp4\""
print upload
upprocess = subprocess.Popen(shlex.split(upload))
(output, err) = upprocess.communicate()
print output
count += 1
@Abdurrafey-Siddiqui
Copy link

Abdurrafey-Siddiqui commented Dec 23, 2022

Hi @acegiak , Is it possible for you to make it like it splits the audio according to where a certain phrase occurs in the video. For example the whenever the word 'test' occurs in the video then split the video and make a separate mp4 vid. Like if 'test' occurs at 10 minutes, the the spliited video will be from 0:00-9:99 in minutes. Hope you understand

@acegiak
Copy link
Author

acegiak commented Dec 23, 2022

Hi, yeah I understand but recognising speech is a much more complex task than just recognising silence, sorry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment