ffmpeg-python
ffmpeg-python copied to clipboard
TypeError: expected str, bytes or os.PathLike object, not OutputStream while merging two mp4 files
I am trying to merge two video files pretending not all the video files would have audio stream and trying to handle it by adding an audio stream when it doesn't exist.
`def merge_videos(file_paths:list, outfile_path:str):
input_streams = []
print("Destination Path: ", outfile_path)
for i, file in enumerate(file_paths):
print("Source file path: ", file)
filename = str(file).split("/")[-1]
input_stream = ffmpeg.input(file)
audio_stream = ffmpeg.probe(file, cmd = 'ffprobe', select_streams='a')
if not audio_stream['streams']:
audio_length = ffmpeg.probe(file)["format"]["duration"]
print(f"Adding audiostream to file {filename} of audio length ", audio_length)
base = ffmpeg.input(f'aevalsrc=0:d={audio_length}', f='lavfi')
concat_file = ffmpeg.concat(input_stream, base, v=1, a=1, unsafe=True)
print("added successfully")
video_streamed_file = ffmpeg.output(concat_file, file.split(".")[0]+"_3."+file.split(".")[1], format = "mp4")
video_streamed_file.run()
input_stream = ffmpeg.input(video_streamed_file)
print(ffmpeg.probe(file.split(".")[0]+"_3."+file.split(".")[1]))
input_video_stream = input_stream.video
input_audio_stream = input_stream.audio
input_streams += (input_video_stream, input_audio_stream)
merged_stream = ffmpeg.concat(*input_streams, v=1, a=1).node
print(merged_stream)
new_path = outfile_path.split(".")[0] + "_3." + outfile_path.split(".")[1]
video = merged_stream[0]
audio = merged_stream[1].filter('volume', 0.8)
output = ffmpeg.output(video, audio, new_path).run()`
Following is the output:
Destination Path: /home/sakshi_gor/repos/Sakshi_WIP_temp/Video_Edit/src/Merged_video.mp4 Source file path: /home/sakshi_gor/repos/Sakshi_WIP_temp/Video_Edit/src/Factory_smoke.mp4 Source file path: /home/sakshi_gor/repos/Sakshi_WIP_temp/Video_Edit/src/Village.mp4 Adding audiostream to file Village.mp4 of audio length 45.612233 added successfully ffmpeg version 4.2.7-0ubuntu0.1 Copyright (c) 2000-2022 the FFmpeg developers built with gcc 9 (Ubuntu 9.4.0-1ubuntu1~20.04.1) configuration: --prefix=/usr --extra-version=0ubuntu0.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-avresample --disable-filter=resample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librsvg --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-nvenc --enable-chromaprint --enable-frei0r --enable-libx264 --enable-shared libavutil 56. 31.100 / 56. 31.100 libavcodec 58. 54.100 / 58. 54.100 libavformat 58. 29.100 / 58. 29.100 libavdevice 58. 8.100 / 58. 8.100 libavfilter 7. 57.100 / 7. 57.100 libavresample 4. 0. 0 / 4. 0. 0 libswscale 5. 5.100 / 5. 5.100 libswresample 3. 5.100 / 3. 5.100 libpostproc 55. 5.100 / 55. 5.100 Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/home/sakshi_gor/repos/Sakshi_WIP_temp/Video_Edit/src/Village.mp4': Metadata: major_brand : mp42 minor_version : 0 compatible_brands: mp42mp41isomavc1 creation_time : 2021-09-28T18:15:27.000000Z Duration: 00:00:45.61, start: 0.000000, bitrate: 5455 kb/s Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 1920x1080, 5452 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 60k tbc (default) Metadata: creation_time : 2021-09-28T18:15:27.000000Z handler_name : L-SMASH Video Handler encoder : AVC Coding Input #1, lavfi, from 'aevalsrc=0:d=45.612233': Duration: N/A, start: 0.000000, bitrate: 2822 kb/s Stream #1:0: Audio: pcm_f64le, 44100 Hz, mono, dbl, 2822 kb/s File '/home/sakshi_gor/repos/Sakshi_WIP_temp/Video_Edit/src/Village_3.mp4' already exists. Overwrite ? [y/N] y Stream mapping: Stream #0:0 (h264) -> concat:in0:v0 Stream #1:0 (pcm_f64le) -> concat:in0:a0 concat:out:a0 -> Stream #0:0 (aac) concat:out:v0 -> Stream #0:1 (libx264) Press [q] to stop, [?] for help [libx264 @ 0x55d0877104c0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2 [libx264 @ 0x55d0877104c0] profile High, level 4.0 [libx264 @ 0x55d0877104c0] 264 - core 155 r2917 0a84d98 - H.264/MPEG-4 AVC codec - Copyleft 2003-2018 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=30 lookahead_threads=5 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00 Output #0, mp4, to '/home/sakshi_gor/repos/Sakshi_WIP_temp/Video_Edit/src/Village_3.mp4': Metadata: major_brand : mp42 minor_version : 0 compatible_brands: mp42mp41isomavc1 encoder : Lavf58.29.100 Stream #0:0: Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, mono, fltp, 69 kb/s (default) Metadata: encoder : Lavc58.54.100 aac Stream #0:1: Video: h264 (libx264) (avc1 / 0x31637661), yuv420p, 1920x1080, q=-1--1, 29.97 fps, 30k tbn, 29.97 tbc (default) Metadata: encoder : Lavc58.54.100 libx264 Side data: cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1 [Parsed_aevalsrc_0 @ 0x55d0876de180] EOF timestamp not reliablerate=4868.2kbits/s speed=0.961x frame= 1367 fps= 28 q=-1.0 Lsize= 28635kB time=00:00:45.62 bitrate=5141.2kbits/s speed=0.93x video:28578kB audio:8kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.173821% [aac @ 0x55d08770ea80] Qavg: 65520.871 [libx264 @ 0x55d0877104c0] frame I:6 Avg QP:19.15 size:421504 [libx264 @ 0x55d0877104c0] frame P:344 Avg QP:21.87 size: 64857 [libx264 @ 0x55d0877104c0] frame B:1017 Avg QP:27.26 size: 4349 [libx264 @ 0x55d0877104c0] consecutive B-frames: 0.8% 0.0% 0.0% 99.2% [libx264 @ 0x55d0877104c0] mb I I16..4: 2.8% 76.3% 20.8% [libx264 @ 0x55d0877104c0] mb P I16..4: 0.2% 0.5% 0.1% P16..4: 40.0% 22.6% 20.0% 0.0% 0.0% skip:16.6% [libx264 @ 0x55d0877104c0] mb B I16..4: 0.0% 0.0% 0.0% B16..8: 46.6% 1.2% 0.2% direct: 0.3% skip:51.7% L0:45.8% L1:50.5% BI: 3.7% [libx264 @ 0x55d0877104c0] 8x8 transform intra:73.8% inter:63.8% [libx264 @ 0x55d0877104c0] coded y,uvDC,uvAC intra: 85.7% 84.3% 47.8% inter: 12.1% 8.2% 0.0% [libx264 @ 0x55d0877104c0] i16 v,h,dc,p: 9% 45% 13% 33% [libx264 @ 0x55d0877104c0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 17% 15% 19% 7% 8% 6% 9% 7% 12% [libx264 @ 0x55d0877104c0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 21% 16% 11% 8% 10% 7% 10% 7% 11% [libx264 @ 0x55d0877104c0] i8c dc,h,v,p: 47% 27% 16% 10% [libx264 @ 0x55d0877104c0] Weighted P-Frames: Y:1.2% UV:0.0% [libx264 @ 0x55d0877104c0] ref P L0: 64.8% 22.6% 10.2% 2.5% 0.0% [libx264 @ 0x55d0877104c0] ref B L0: 93.8% 5.7% 0.5% [libx264 @ 0x55d0877104c0] ref B L1: 94.6% 5.4% [libx264 @ 0x55d0877104c0] kb/s:5132.46 {'streams': [{'index': 0, 'codec_name': 'aac', 'codec_long_name': 'AAC (Advanced Audio Coding)', 'profile': 'LC', 'codec_type': 'audio', 'codec_time_base': '1/44100', 'codec_tag_string': 'mp4a', 'codec_tag': '0x6134706d', 'sample_fmt': 'fltp', 'sample_rate': '44100', 'channels': 1, 'channel_layout': 'mono', 'bits_per_sample': 0, 'r_frame_rate': '0/0', 'avg_frame_rate': '0/0', 'time_base': '1/44100', 'start_pts': 0, 'start_time': '0.000000', 'duration_ts': 2011489, 'duration': '45.611995', 'bit_rate': '1402', 'max_bit_rate': '69000', 'nb_frames': '1966', 'disposition': {'default': 1, 'dub': 0, 'original': 0, 'comment': 0, 'lyrics': 0, 'karaoke': 0, 'forced': 0, 'hearing_impaired': 0, 'visual_impaired': 0, 'clean_effects': 0, 'attached_pic': 0, 'timed_thumbnails': 0}, 'tags': {'language': 'und', 'handler_name': 'SoundHandler'}}, {'index': 1, 'codec_name': 'h264', 'codec_long_name': 'H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10', 'profile': 'High', 'codec_type': 'video', 'codec_time_base': '1001/60000', 'codec_tag_string': 'avc1', 'codec_tag': '0x31637661', 'width': 1920, 'height': 1080, 'coded_width': 1920, 'coded_height': 1088, 'has_b_frames': 2, 'pix_fmt': 'yuv420p', 'level': 40, 'chroma_location': 'left', 'refs': 1, 'is_avc': 'true', 'nal_length_size': '4', 'r_frame_rate': '30000/1001', 'avg_frame_rate': '30000/1001', 'time_base': '1/30000', 'start_pts': 0, 'start_time': '0.000000', 'duration_ts': 1368367, 'duration': '45.612233', 'bit_rate': '5132579', 'bits_per_raw_sample': '8', 'nb_frames': '1367', 'disposition': {'default': 1, 'dub': 0, 'original': 0, 'comment': 0, 'lyrics': 0, 'karaoke': 0, 'forced': 0, 'hearing_impaired': 0, 'visual_impaired': 0, 'clean_effects': 0, 'attached_pic': 0, 'timed_thumbnails': 0}, 'tags': {'language': 'und', 'handler_name': 'VideoHandler'}}], 'format': {'filename': '/home/sakshi_gor/repos/Sakshi_WIP_temp/Video_Edit/src/Village_3.mp4', 'nb_streams': 2, 'nb_programs': 0, 'format_name': 'mov,mp4,m4a,3gp,3g2,mj2', 'format_long_name': 'QuickTime / MOV', 'start_time': '0.000000', 'duration': '45.636000', 'size': '29322433', 'bit_rate': '5140228', 'probe_score': 100, 'tags': {'major_brand': 'isom', 'minor_version': '512', 'compatible_brands': 'isomiso2avc1mp41', 'encoder': 'Lavf58.29.100'}}} concat(a=1, n=2, v=1) <9cd057c54940> Traceback (most recent call last): File "/home/sakshi_gor/repos/Sakshi_WIP_temp/Video_Edit/Video_edit_ffmpeg.py", line 69, in <module> print(merge_videos(vid_files, output_file)) File "/home/sakshi_gor/repos/Sakshi_WIP_temp/Video_Edit/Video_edit_ffmpeg.py", line 34, in merge_videos output = ffmpeg.output(video, audio, new_path).run() File "/home/sakshi_gor/.local/share/virtualenvs/Sakshi_WIP_temp/lib/python3.8/site-packages/ffmpeg/_run.py", line 313, in run process = run_async( File "/home/sakshi_gor/.local/share/virtualenvs/Sakshi_WIP_temp/lib/python3.8/site-packages/ffmpeg/_run.py", line 284, in run_async return subprocess.Popen( File "/usr/lib/python3.8/subprocess.py", line 858, in __init__ self._execute_child(args, executable, preexec_fn, close_fds, File "/usr/lib/python3.8/subprocess.py", line 1639, in _execute_child self.pid = _posixsubprocess.fork_exec( TypeError: expected str, bytes or os.PathLike object, not OutputStream
Error specific log:
Traceback (most recent call last): File "/home/sakshi_gor/repos/Sakshi_WIP_temp/Video_Edit/Video_edit_ffmpeg.py", line 69, in <module> print(merge_videos(vid_files, output_file)) File "/home/sakshi_gor/repos/Sakshi_WIP_temp/Video_Edit/Video_edit_ffmpeg.py", line 34, in merge_videos output = ffmpeg.output(video, audio, new_path).run() File "/home/sakshi_gor/.local/share/virtualenvs/Sakshi_WIP_temp/lib/python3.8/site-packages/ffmpeg/_run.py", line 313, in run process = run_async( File "/home/sakshi_gor/.local/share/virtualenvs/Sakshi_WIP_temp/lib/python3.8/site-packages/ffmpeg/_run.py", line 284, in run_async return subprocess.Popen( File "/usr/lib/python3.8/subprocess.py", line 858, in __init__ self._execute_child(args, executable, preexec_fn, close_fds, File "/usr/lib/python3.8/subprocess.py", line 1639, in _execute_child self.pid = _posixsubprocess.fork_exec( TypeError: expected str, bytes or os.PathLike object, not OutputStream
I tried to look into the library documentation, FFMPEG documentation, Googling, ChatGPT as well but couldn't find the solution. Thank you