joplin-notes/joplin notion extension/Web Development/Extras/ffmpeg.md
2026-11-04 00:33:23 +05:30

6.3 KiB

ffmpeg

  • For recording a particular section of video https://superuser.com/questions/639402/convert-specific-video-and-audio-track-only-with-ffmpeg ffmpeg -i input.video -ss hh:mm:ss.ms -t ss output.video Eg : ffmpeg -i inputVideo.mkv -ss 00:34:12.00 -t 20 outputVideo.mkv This will start recording from 34th min 12th sec to 34th min 32sec as 20 after -t is 20s

  • Choosing codecs, audio tracks ffmpeg -i Mr.Robot.S01E03.720p.Hindi.English.moviesFlixPro.in.mkv -ss 00:35:18.00 -t 50 -c:v libx264 -c:a ac3 -crf 20 -map 0:0 -map 0:1 recorded1.mp4

    -c:v : video codec libx264 means popular H.264 -c:a audio codec ac3 is audio codec -crf : constant rate factor map is for stream id so 0 represents 1st, 1 represents 2nd and so on. but this can be overwelming and confusing t know which one is audio stream or video stream or subtitles or something else

    ffmpeg -i inputVideo.mkv -ss 00:35:18.00 -t 50 -c:v libx264 -c:a ac3 -crf 20 -map 0:v:0 -map 0:a:1 recorded1.mp4 -map 0:v:0 means 1st video track -map 0:a:1 means 2nd audio track so if there r 2 audio track 2nd audio track will be encoded ac3 audio track may not work in browser so choose aac as audio track or ffmpeg -i "Texas Police Chief arrests woman over facebook post warning neighbors about dirty tap water.mp4" -ss 00:07:00.00 -t 20 -c:v libx264 -crf 20 -map 0:v:0 -map 0:a:0 life_is_political.mp4

  • extracting video including subtitle

    • first extract subtitle file ffmpeg -i input.mkv -map 0:s:0 -c:s srt subtitles.srt or ffmpeg -i input.mkv subtitles.srt

    • then use some script or use online tool like https://subshifter.bitsnbites.eu/ to offset subtitles respective to time range of video extracted

      import re
      
       def adjust_timing(srt_file, haste_seconds):
       	with open(srt_file, 'r') as file:
       		content = file.readlines()
      
       	adjusted_content = []
       	for line in content:
       		# Match timestamps
       		if re.match(r'\d\d:\d\d:\d\d,\d{3} --> \d\d:\d\d:\d\d,\d{3}', line):
       			start, end = line.split(' --> ')
       			# Adjust both start and end times
       			new_start = adjust_time(start, haste_seconds)
       			new_end = adjust_time(end.strip(), haste_seconds)
       			adjusted_content.append(f"{new_start} --> {new_end}\n")
       		else:
       			adjusted_content.append(line)
      
       	with open('adjusted_subtitles.srt', 'w') as file:
       		file.writelines(adjusted_content)
      
       def adjust_time(timestamp, haste_seconds):
       	hours, minutes, seconds = map(float, re.split('[:,]', timestamp))
       	total_seconds = hours * 3600 + minutes * 60 + seconds - haste_seconds
       	if total_seconds < 0:
       		total_seconds = 0  # Prevent negative timestamps
       	new_hours = int(total_seconds // 3600)
       	new_minutes = int((total_seconds % 3600) // 60)
       	new_seconds = total_seconds % 60
       	return f"{int(new_hours):02}:{int(new_minutes):02}:{new_seconds:06.3f}".replace('.', ',')
      
       adjust_timing('subtitles.srt', 300)  # Hasten by 5 minutes (300 seconds)
      
      
    • then extract video without subtitle like before then merge subtitle ffmpeg -i input.mkv -ss 00:35:18.00 -t 50 -crf 20 -map 0:v:0 -map 0:a:0 recorded1.mp4 and ffmpeg -i recorded1.mp4 -i subtitles-resync.srt -c copy -c:s mov_text outfile.mp4 and here this outfile.mp4 is desired file with extract video,audio and subtitles  

  • convert video to audio ffmpeg -i video.mp4 -b:a 192K -vn music.mp3 or ffmpeg -i video.mp4 -vn music.mp3 here -vn means no video -b:a 192K : to target audio stream to tell it for 192k bitrate   for converting all videos in folder to audio

    for arg in * ; do 
       ffmpeg -i "${arg}" -vn  "${arg%%.*}.mp3"
    done
    

     

** get information about multimedia file** ffprobe -i input.mp4  

merging all videos https://www.mux.com/articles/stitch-multiple-videos-together-with-ffmpeg

  • create inputs.txt file and add the videos to be merged
      file 'video1.mp4'
      file 'video2.mp4'
      file 'video3.mp4'
    
    
  • then simply run this ffmpeg -f concat -safe 0 -i inputs.txt -c copy output.mp4 -f concat: Specifies the concatenation format. -safe 0: Prevents file path errors if your filenames use relative or absolute paths. -c copy: Copies the video and audio streams directly without re-encoding, preserving 100% quality.

Method 2: Different Formats (Requires Re-encoding) If your clips have different resolutions, frame rates, or codecs, you must use a Complex Filtergraph. This re-encodes the segments so they blend smoothly into a single unified format. [2, 8] Run this single command to merge two mismatched files: [9, 10, 11]

ffmpeg -i video1.mp4 -i video2.mp4 -filter_complex "[0:v][0:a][1:v][1:a] concat=n=2:v=1:a=1 [v][a]" -map "[v]" -map "[a]" output.mp4

or

ffmpeg -i video_480p.mp4 -i video_720p.mp4 -filter_complex \ "[0:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2[v0]; \ [v0][0:a][1:v][1:a]concat=n=2:v=1:a=1[v][a]" \ -map "[v]" -map "[a]" -c:v libx264 -crf 23 -c:a aac final_output.mp4

  • -i video1.mp4 -i video2.mp4: Specifies the input files sequentially.
  • [0:v][0:a][1:v][1:a]: Tells FFmpeg to extract the video and audio streams from input 0 (first file) and input 1 (second file).
  • concat=n=2:v=1:a=1: Combines the 2 input segments (n=2) into 1 output video stream (v=1) and 1 output audio stream (a=1).
  • -map "[v]" -map "[a]": Maps the newly joined streams to the final output file. [8, 9, 12]  

or if only 1 file is being different then only convert that one like others 480 one converted to like of 720p one here ffmpeg -i video_480p.mp4 -i video_720p.mp4 -filter_complex \ "[0:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2[v]" \ -map "[v]" -map 0:a -c:v libx264 -c:a aac converted_480p.mp4

then later merge them with others. though this will work if they were using those libx264 and aac audio. to be more safer some manual work like finding audio and video encoding via ffprobe. so for the witcher 3 recorded videos it was

Video: h264 (Main) (avc1 / 0x31637661), yuv420p(progressive), 1920x1080 [SAR 1:1 DAR 16:9], 10006 kb/s, 28.91 fps, 60 tbr, 30k tbn (default)

and to convert 480p ones to witcher one ffmpeg -i video_480p.mp4 -vf "scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2" -r 28.91 -c:v libx264 -profile:v main -c:a copy converted_480p.mp4